如何使用reloadRowsAtIndexPath更新行 [英] How update rows with reloadRowsAtIndexPath
问题描述
我正在尝试用Button删除单元格。
I'm trying to delete cell with a Button.
这是单元格的实现。
- (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 $ c之后放入
reloadRowsAtIndexPaths:withRowAnimation:
for visible indexPath
$ c>方法,效果很好。但是reloadRows动画使删除动画变得丑陋(即使我将其设置为NO)。
所以有没有办法在没有reloadCellsAtIndexPath的情况下重新加载单元格。或者在不使用标签的情况下删除行。
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.
这篇关于如何使用reloadRowsAtIndexPath更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!