xcode commitEditingStyle 的问题 - 删除表格中的单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6857711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem with commitEditingStyle -to delete cell in the table
提问by Nimit Parekh
i have one section table which contain the section and i want to delete the perticular cell swap and delete this cell and remove that cell from table and also remove from the array.
我有一个包含该部分的部分表,我想删除特定单元格交换并删除此单元格并从表中删除该单元格并从数组中删除。
and how to animate that when the delete the cell.
以及如何在删除单元格时设置动画。
for that i code below but which is not working please help to do this.
对于我下面的代码,但它不起作用,请帮助做到这一点。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.reports removeObjectAtIndex:indexPath.row];
[tbl reloadData];
[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
After much more R&D then i build up new code and which is successfully Run
经过更多的研发,然后我建立了新的代码并成功运行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[[[self.reports objectAtIndex:indexPath.section] valueForKey:@"events"] removeObjectAtIndex:indexPath.row];
[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationLeft];
[NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(tableReload) userInfo: nil repeats: NO];
}
}
}
-(void)tableReload{
[tbl reloadData]; }
回答by Nitish
Firstly you should not Reload table
before deleting row from table. Secondly, in your case you have multiple sections and multiple rows in that section, I guess. So, your
首先,您不应该Reload table
在从表中删除行之前。其次,在您的情况下,我猜您在该部分有多个部分和多行。所以,你的
[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
won't work.
Deleting object from array will work. You don't have to delete row from your table. Just reload table :
不会工作。
从数组中删除对象将起作用。您不必从表中删除行。只需重新加载表:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.reports removeObjectAtIndex:indexPath.row];
[tbl reloadData];
}
But do take care that you are putting the correct indexNumber.
但是请注意您放置的是正确的 indexNumber。
回答by Suhail Patel
You are not nil terminating your NSArray and that is why the row isn't being deleted from your Table View.
你不是 nil 终止你的 NSArray,这就是为什么没有从你的表视图中删除该行。
The following line:
以下行:
[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
Should actually be:
实际上应该是:
[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationFade];
Notice the nil after IndexPath. arrayWithObjects should be a nil terminated list of Objects
注意 IndexPath 后面的 nil。arrayWithObjects 应该是一个 nil 终止的对象列表