开发者

How update rows with reloadRowsAtIndexPath

开发者 https://www.devze.com 2023-02-11 12:33 出处:网络
I\'m trying to delete cell with a Button. Here is the implementation of a cell. - (UITableViewCell *)tableView:(UITableView *)tableView

I'm trying to delete cell with a Button.

Here is the implementation of a cell.

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell开发者_如何学C = [[[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];
}

This works well until I've delete a row, the button isn't reloaded so after a delete, I don't delete the good index. I tried to put reloadRowsAtIndexPaths:withRowAnimation: for visible indexPath after the endUpdates method and it works very well. But the delete animation became ugly by the reloadRows animation (even if I set it to NO). So is there a way to reload cells without reloadCellsAtIndexPath. Or delete a rows without using tags.

Thx a lot.


You should be able to lookup the index of the cell by using logic similar to the following:

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

With this approach, you would not need to store the value of the row index in the cell tag.


To just reload the table (and thus all its cells), you could try just using:

[_tableView reloadData];
0

精彩评论

暂无评论...
验证码 换一张
取 消