2012-06-14 4 views
0

간단한 타이머 앱을 만들고 있습니다. 여러 행이있는 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 
+1

시도 button.tag = indexPath.row + someoffset; 0 행이 문제 일 수 있습니다. – thelaws

+0

@thelaws - 감사합니다. 당신이 바로 행 0 나를 죽이고 있었어 –

답변

2

에서 얻을 오류입니다. 이 경우 viewWithTag:0을 호출하지만 UIView의 태그는 기본적으로 0이므로 셀의 하위보기에서 UIButton뿐 아니라 값이 0 인 태그가있을 가능성이 있습니다.

이 문제를 방지하려면 태그에 영향을 미치기 전에 indexPath.row에 상수 (100과 같은)를 추가하여 행 0의 버튼에 0이 아닌 태그 100을 갖도록하십시오 (행 1의 버튼 0

+0

정말 고마워. 그것은 트릭을했다. 나는 당신의 제안과 행복에 따라 코드에 100을 더했습니다. 휴! button.tag = (indexPath.row) +100; 버튼 = (UIButton *) [셀 viewWithTag : ((indexPath.row) +100)]; –

1

그래이 때 indexPath 셀을 반환 행 0

button = (UIButton *) [cell viewWithTag:indexPath.row]; 

실패 할 태그 (101)이, 등 ...),이 태그가 다른 하위 뷰 혼동 피하는 것 cell.tag가 0이기 때문에 .row는 0입니다.


일반적으로이 방법은 효과가 없습니다. 처음으로 셀을 재사용하면 버튼에 잘못된 태그가 생깁니다. 예를 들어, 18 행의 2 행에서 셀을 다시 사용하면 단추의 태그는 2가되지만 찾은 태그는 18입니다. nil을 반환합니다.

왜 상수를 #define kMyStopStartButtonTag 12345으로 정의하고 전체 상수를 사용하지 않는가?

if (cell == nil) 
{ 
    … 
    button.tag = kMyStopStartButtonTag; 
    [cell addSubview:button]; 
    … 
} else { 
    button = (UIButton *) [cell viewWithTag:kMyStopStartButtonTag]; 
} 
+0

상수를 정의 할 수없는 이유는 셀의 각 행 옆에 단추가 있고 사용자가 어떤 단추를 눌렀는지 즉, 시작/중지하려는 사용자가 무엇인지 알 필요가 있기 때문입니다. –

+0

@SamBudda'[self. (IBAction) timerStopStart : (UIButton *) 버튼;에서 'tableView indexPathForCell : (UITableViewCell *) button.superview]'를 호출하여 버튼에서 셀의 indexPath를 가져옵니다. –