【问题标题】:How update rows with reloadRowsAtIndexPath如何在 IndexPath 使用 reloadRows 更新行
【发布时间】:2011-06-27 01:55:54
【问题描述】:

我正在尝试使用按钮删除单元格。

这是一个单元格的实现。

- (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 的情况下重新加载单元格。或者不使用标签删除一行。


非常感谢。

【问题讨论】:

    标签: iphone objective-c ipad uitableview delete-row


    【解决方案1】:

    要重新加载表格(以及所有单元格),您可以尝试使用:

    [_tableView reloadData];
    

    【讨论】:

    • 我也试过了,效果很好。但是删除的动画就不一样了。尝试有无,你可以看到两者之间的区别......
    【解决方案2】:

    您应该能够使用类似于以下的逻辑来查找单元格的索引:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多