在项目中UITableCell经常涉及到删除cell的功能,如果设置失当就会造成崩溃后果,这里的设置主要是指cell的数量,以及数据源的变动,其牵涉到的代理主要要有:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// NSLog(@"section number::%tu",[self.indexArr count]);
return [self.indexArr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}另外需要特别注意的是删除cell需要分两种情况,第一种为纯粹删除cell,如下图:
调用这个接口:
[tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
另一种是连带section也要删除,如下图:
则一定要调用这个接口:
[tableView deleteSections:[NSIndexSetindexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationLeft];
本文详细介绍了在使用Swift开发时,如何正确处理UITableView中删除cell的功能,避免因不当设置导致的崩溃问题。文章通过具体代码示例展示了如何计算cell数量、更新数据源以及在不同场景下正确调用删除cell接口,确保应用稳定运行。
1418

被折叠的 条评论
为什么被折叠?



