在项目中删除cell这个功能用到的地方很多,下面是系统删除和点击删除两种
1.系统协议方法删除:
返回删除协议方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
遵守协议方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_numberArray removeObjectAtIndex:indexPath.section];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[self.tableView reloadData];
}
2.自定义button删除
首先设置button的tag值为index path.section
然后在button的点击方法里面实现
NSIndexPath *index = [NSIndexPath indexPathWithIndex:sender.tag];
[self tableView:self.tableViewcommitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:index];
[self.tableView reloadData];
这里需要说明一下,上面方法是删除section的方法,如果是row的话只需改成row,[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; 这个方法要换成删除index的方法.记得要加刷新