`
jsntghf
  • 浏览: 2483230 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

TableView中的Delete操作

    博客分类:
  • iOS
阅读更多

只要实现UITableViewDataSource接口中的tableView:commitEditingStyle:forRowAtIndexPath:方法即可。

 

#pragma mark -
#pragma mark Table View Data Source Methods
- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {    
    NSUInteger row = [indexPath row];
    [self.list removeObjectAtIndex:row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                     withRowAnimation:UITableViewRowAnimationFade];
}

 

如果UITableView是UITableViewStyleGrouped类型的话,当删除分组中最后一条数据时,该分组也必须删除,否则会报错:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070

libc++abi.dylib: handler threw exception

 

以下代码示例多个分组,每个分组一条数据的情况

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.list removeObjectAtIndex:indexPath.section];
    
    [tableView beginUpdates];
    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];
}

 

分享到:
评论
2 楼 jsntghf 2011-08-10  
hhb19900618 写道
大哥 这个函数:commitEditingStyle是干啥的啊?

在编辑过程中,用户可以从表格中删除记录。用户会看到一个删除确认信息。确认之后,数据源的commitEditingStyle方法就会得到通知,从而令应用程序得知有一个删除请求。这个方法也属于UITableViewDataSource协议。你需要从数据源中删除对应的数据,来满足这个请求。
1 楼 hhb19900618 2011-08-10  
大哥 这个函数:commitEditingStyle是干啥的啊?

相关推荐

Global site tag (gtag.js) - Google Analytics