2013-01-17 2 views
2

사용자 지정 UITableViewCellAccessoryCheckmark가 필요한 테이블보기가 있습니다. 체크 표시는 행을 선택하면 표시되고 다른 행이 선택되면 마지막으로 선택한보기에 표시됩니다. 그건 잘 작동합니다.사용자 지정 UITableViewCellAccessoryCheckmark를 표시하는 방법

cell.accessoryView = [[ UIImageView alloc ] 
          initWithImage:[UIImage imageNamed:@"icon-tick.png" ]]; 

사용자 정의 UITableViewCellAccessoryCheckmark를 추가 :이 줄을 사용하는 경우

문제

가 발생합니다. 이 코드 다음에 UITableViewCellAccessoryCheckmark는 모든 행에 남아 있고 다른 행을 터치해도 사라지지 않습니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

int index = indexPath.row; id obj = [listOfItems objectAtIndex:index]; 

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

NSLog(@"%d",indexPath.row); 
if (rowNO!=indexPath.row) { 
    rowNO=indexPath.row; 
    [self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark; 

    cell.accessoryView = [[ UIImageView alloc ] 
          initWithImage:[UIImage imageNamed:@"icon-tick.png" ]]; 

    [self.tableView cellForRowAtIndexPath:lastIndexPth].accessoryType=UITableViewCellAccessoryNone; 
    lastIndexPth=indexPath; 
} 
+0

당신이 cicle에 대한이 링크는 또한 당신을 위해 유용 다른 모든 세포 –

+0

사라를 위해 작성해야합니다 http://stackoverflow.com/questions/6359043/is-uitableviewcellaccessory 체크 표시가있는 이미지 –

+0

이 링크는 또한 유용합니다. http://stackoverflow.com/questions/6359043/is-uitableviewcellaccessorycheckmark-an-image –

답변

8

훨씬 깨끗하고 시원한 방법은 다음과 같이있는 UITableViewCell을 덮어하는 것입니다 : 당신이 당신의 클래스가 자동으로 이미지로 교체하기 때문에 당신이 UITableViewCellAccessoryCheckmark을 계속 사용할 수 있다는했을 경우

- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType 
{ 
    // Check for the checkmark 
    if (accessoryType == UITableViewCellAccessoryCheckmark) 
    { 
     // Add the image 
     self.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImage.png"]] autorelease]; 
    } 
    // We don't have to modify the accessory 
    else 
    { 
     [super setAccessoryType:accessoryType]; 
    } 
} 

.

cellForRowAtIndexPath 방법으로 만 스타일을 설정해야합니다. 이처럼 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // [init subclassed cell here, dont forget to use the table view cache...] 

    cell.accessoryType = (rowNO != indexPath.row ? nil : UITableViewCellAccessoryCheckmark); 

    return cell; 
} 

그 다음을, 당신은이 같은 데이터를 업데이트하고 셀을 다시 그려야 didSelectRowAtIndexPathrowNO을 업데이트 할 수 있습니다 또한

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (rowNO != indexPath.row) 
    { 
     rowNO = indexPath.row; 
    } 

    [self.tableView reloadData]; 

} 

을 대신 [self.tableView reloadData]로 전체 테이블을 다시로드의 reloadRowsAtIndexPaths을 사용하여 스타일을 변경하는 두 개의 셀 (예 : 체크 표시) 만 다시로드 할 수 있습니다.

3

흠 나는 왜 그런지 모르지만 나는 답을 쓰고있다. Blauesocke의 문제는 AccessoryType이 UITableViewCellAccessoryCheckmark로 설정되지 않아 셀 AccessoryType을 확인할 수 없다는 것입니다. 거기에 바로 할 수있는 방법이 있나요 그래서 세포 AccessoryType은 또 다른 이미지를 corect 유형 것입니다.

이 같은 방법을 사용하고 있습니다 :

- (void)setAccessoryType:(UITableViewCellAccessoryType)newAccessoryType 
{ 
    [super setAccessoryType:newAccessoryType]; 
    // Check for the checkmark 
    switch(newAccessoryType) 
    { 
     case UITableViewCellAccessoryCheckmark: 
      self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yorCheckmark.png"]]; 
      break; 
     case UITableViewCellAccessoryNone: 
      self.accessoryView = nil; 
      break; 
     default: 
      break; 
    } 

} 
+0

왜 액세서리 유형을 확인 하시겠습니까? –

+0

셀을 선택 취소하거나 확인하는 데 사용하고 있습니다. 네, 그렇게 할 다른 가능성이 있다는 것을 압니다. 그러나 저는 그 코드를 사용하여 아이콘을 변경하라는 요청을 받았습니다. 그래서 대답은 내가 오래된 코드를 변경하고 싶지 않다는 것입니다. :) – kubo

+0

대단한 !!! 감사 – tesmojones

관련 문제