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.
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];
精彩评论