【发布时间】: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