我试图用按钮删除单元格。如何用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很多。
非常聪明的方式,它的工作。多谢 – klefevre 2011-02-14 17:56:12