2011-02-14 7 views
2

버튼이있는 셀을 삭제하려고합니다.reloadRowsAtIndexPath를 사용하여 행을 업데이트하는 방법

다음은 셀의 구현입니다.

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 

     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn.frame = CGRectMake(0.f, 0.f, 30.f, 30.f); 
     [btn addTarget:self 
       action:@selector(btnTouched:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:btn]; 
     cell.tag = indexPath.row; 
    } 
    return cell; 
} 

- (void)btnTouched:(UIButton *)sender 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 

    [_myMutableArray removeObjectAtIndex:sender.tag]; 
    [_tableView beginUpdates]; 
    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationLeft]; 
    [_tableView endUpdates]; 
} 

버튼을 다시로드 할 때까지 제대로 작동합니다. 삭제 후에 버튼이 다시로드되지 않기 때문에 좋은 색인은 삭제되지 않습니다. 메서드 다음에 visible indexPath에 대해 reloadRowsAtIndexPaths:withRowAnimation:을 넣으려고했는데 아주 잘 작동합니다. 그러나 삭제 애니메이션은 reloadRows 애니메이션에 의해 추악 해졌습니다 (비록 NO로 설정하더라도). 그래서 reloadCellsAtIndexPath없이 셀을 다시로드하는 방법이 있습니다. 또는 태그를 사용하지 않고 행을 삭제하십시오.


Thx 많이 있습니다.

답변

3

당신은 다음과 같은 논리를 사용하여 셀의 인덱스를 조회 할 수 있어야한다 :

NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell *)[sender superview]]; 

이 방법을 사용하면 행 인덱스의 값을 셀 태그에 저장할 필요가 없습니다.

+0

매우 똑똑한 방식으로 작동했습니다. 정말 고마워 – klefevre

0

그냥 테이블을 다시로드하려면 (따라서 모든 셀), 당신이 사용하는 단지 시도 할 수 :

[_tableView reloadData]; 
+0

나는 또한 그것을 시도하고 아주 잘 작동합니다. 그러나 삭제의 애니메이션은 동일하지 않습니다. 함께 시도해 보시고 두 가지의 차이점을보실 수 있습니다 ... – klefevre

관련 문제