我在UITableView中删除行的概念存在小问题。用户删除一行后我不想触发:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
相反,我希望更改已编辑的单元格文本和背景而不隐藏它。我的方法很有效:
if (editingStyle == UITableViewCellEditingStyleDelete) { NSMutableArray *previous_points = [self.trip_arrayOfAllPoints mutableCopy]; [previous_points removeObjectAtIndex:indexPath.row]; self.trip_arrayOfAllPoints = nil; self.trip_arrayOfAllPoints = [[NSMutableArray alloc] initWithCapacity:[previous_points count]]; self.trip_arrayOfAllPoints = [previous_points mutableCopy]; self.trip_arrayOfAllPointsEDITED = [[NSMutableArray alloc] initWithCapacity:[previous_points count]]; self.trip_arrayOfAllPointsEDITED = [previous_points mutableCopy]; //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; self.trip_isArrayOfAllPointsEDITED = YES; }
但删除一行后,它不会在当前单元格上提交消除“DELETE”标签的动画。有没有办法围绕“Apple方式”删除行?
编辑 - 我已经解决了
这很简单明了。只需将deleteRowsAtIndexPaths替换为:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
此“删除”动画消失后,单元格仍在其中。
答案 0 :(得分:2)
你的意思是“删除”按钮会保留,即使你按下了它?在我的iOS 5.1模拟器之后 按“删除”键为空:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
“删除”按钮消失。
答案 1 :(得分:1)
我正在使用此代码并且工作正常
//self.selectedMessageIndex is NSIndexPath object declared in header file
//self.attachments is a NSMutableArray Object
[self.attachments removeObjectAtIndex:self.selectedMessageIndex.row];
[tblvAttachmentsList deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.selectedMessageIndex]
withRowAnimation:UITableViewRowAnimationLeft];
[tblvAttachmentsList reloadData];
答案 2 :(得分:0)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:.Automatic)
这将删除带有漂亮动画的删除按钮。当您知道不打算删除该行时调用:
func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath:NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
//An action happens here. If it was successful, page is refreshed
//If the action fails, cell stays. I am showing an error and:
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:.Automatic)
}
}