간단한 타이머 앱을 만들고 있습니다. 여러 행이있는 uitable 있습니다. 그 테이블에는 UIButton을 넣었다. 모든 기능이 작동하며 지금까지 버튼이 각 행에 표시됩니다. 다음으로해야 할 일은 UIButton에 대한 이미지를 설정하는 것입니다. 타이머가 실행 중이라면 UIButton에 stop_button_image를 보여주고 싶었습니다. 그리고 타이머가 시작 되었다면 버튼의 UIImage를 start_button_image로 설정하고 싶었습니다UITableCellView의 UIButton 이미지 문제
문제는 테이블이 새로 고침되면 더러운 추적을 얻는 것입니다. 내가 UIButton setimage 코드를 어디에 두 었는지. 다음은 참조 용 코드입니다.
//WORKING CODE: (NOT THAT I WANT)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//TESTING - Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell == nil)
{
// Use the default cell style.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//TESTING - Button
[button addTarget:self
action:@selector(timerStopStart:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Start/Stop" forState:UIControlStateNormal];
button.frame = CGRectMake(5.0f, 5.0f, 72.0f, 77.0f);
button.tag = indexPath.row;
[cell addSubview:button];
//THIS WORKS IF I PUT THIS HERE
[button setImage:[UIImage imageNamed:@"button_pause.png"] forState:UIControlStateNormal];
}
else
{
//NSLog(@"went here 2");
button = (UIButton *) [cell viewWithTag:indexPath.row];
}
//some other logic
}
// 코드 HERE 나누기
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//TESTING - Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell == nil)
{
// Use the default cell style.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//TESTING - Button
[button addTarget:self
action:@selector(timerStopStart:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Start/Stop" forState:UIControlStateNormal];
button.frame = CGRectMake(5.0f, 5.0f, 72.0f, 77.0f);
button.tag = indexPath.row;
[cell addSubview:button];
}
else
{
//NSLog(@"went here 2");
button = (UIButton *) [cell viewWithTag:indexPath.row];
}
//CODE BREAKS HERE
[button setImage:[UIImage imageNamed:@"button_pause.png"] forState:UIControlStateNormal];
//some other logic
}
문제가 테이블이 갱신 될때는 코드 버튼 구역 호출이다 = (있는 UIButton *) 셀 viewWithTag : indexPath.row]; 내가 아는 것은 단지 참고서입니다.
다음은 iOS 시뮬레이터에서 발생하는 오류입니다. 그 이미지 코드를 셀 바깥에 넣고 싶습니다. == null 체크. 어떻게 이런 일을 할 수있는 단서? 여기
는 indexPath.row가 제로의 경우 문제는 아마 내가 시뮬레이터2012-06-14 12:39:27.246 Timers[5381:10a03] -[UITableViewCell setImage:forState:]: unrecognized selector sent to instance 0x73706e0
2012-06-14 12:39:27.247 Timers[5381:10a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setImage:forState:]: unrecognized selector sent to instance 0x73706e0'
*** First throw call stack:
(0x150b052 0x16bbd0a 0x150cced 0x1471f00 0x1471ce2 0x136c6 0x7b5e0f 0x7b6589 0x7a1dfd 0x7b0851 0x75b322
시도 button.tag = indexPath.row + someoffset; 0 행이 문제 일 수 있습니다. – thelaws
@thelaws - 감사합니다. 당신이 바로 행 0 나를 죽이고 있었어 –