我試圖用按鈕刪除單元格。如何用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