2014-02-28 5 views
0

UITableView에서 _selectAttributes라는 이름을 붙 였는데 각 셀을 탭할 때 체크 표시를 제거하고 싶습니다. 이 코드입니다 :UITableView 셀의 체크 표시

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell * cell = [_selectAttributes cellForRowAtIndexPath:indexPath]; 
    if (cell.accessoryType != UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
} 

모든 것이 잘 작동하는 것 같다,하지만 난 스크롤 및 테이블 아래로, 체크 표시가 다른 세포에 표시하고 이전 것들에 사라질 때.

어떻게 해결할 수 있습니까?

미리 감사드립니다.

+0

가능한 복제본 [UITableViewCellAccessory가 화면을 벗어날 때 사라짐] (http://stackoverflow.com/questions/5827034/uitableviewcellaccessory-disappears-when-scrolled-off-screen) – rmaddy

답변

1

selectedIndexPath이라는 NSIndexPath 속성을 선언하십시오.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 

    if ([indexPath isEqual:self.selectedIndexPath]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.selectedIndexPath]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    self.selectedIndexPath = indexPath; 
} 

UPDATE :

는 그런 다음 대리자 메서드이 같은 cellForRowAtIndexPathdidSelectRowAtIndexPath가 나는 여러 셀 선택에 대한 솔루션을 원하는 관심을 지불하지 않은 합니다. 내 대답은 분명히 단일 셀 선택에 대해서만 문제를 해결하지만, 나는 그것의 좋은 시작을 믿습니다.

+0

솔루션을 배열로 적용하여 해결했습니다! – charles

1

선택한 인덱스 경로의 NSArray를 만들 것입니다. tableView:didSelectRowAtIndexPath:에 해당 배열에 인덱스 경로를 추가하고 tableView:cellForRowAtIndexPath:에 인덱스 경로가 배열에 있는지 확인하고 그에 따라 UITableViewCell의 액세서리 유형을 설정하십시오.

+0

그런데보고있는 동작은 다음과 같습니다. UITableView가 셀을 재사용하는 방법의 결과. 테이블 뷰는 성능상의 이유로이 작업을 수행하지만 여러 가지 방법으로 두통을 유발할 수 있습니다. 당신이 이미 아니라면 개념으로 세포 재사용에 익숙해 지 것을 권하고 싶습니다. – geraldWilliam