2011-02-14 59 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]; 
} 

这很好的工作,直到我删除了一行,按钮没有重新加载,所以删除后,我不删除好索引。我试图在endUpdates方法后面输入reloadRowsAtIndexPaths:withRowAnimation:作为visible indexPath,它工作得很好。但是,reloadRows动画使删除动画变得丑陋(即使我将它设置为NO)。 所以有没有reloadCellsAtIndexPath重新加载单元格的方法。或者在不使用标签的情况下删除行。


Thx很多。

回答

3

您应该能够通过使用类似下面的逻辑来查找单元的索引:

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

使用这种方法,您不需要将行索引的值存储在单元标记中。

+0

非常聪明的方式,它的工作。多谢 – klefevre 2011-02-14 17:56:12

0

要刚刚重装该表(因此它的所有细胞),你可以使用尝试只:

[_tableView reloadData]; 
+0

我也试过它,它工作得很好。但删除的动画并不相同。尝试有和没有,你可以看到两者之间的区别... – klefevre 2011-02-14 17:47:20