好久以前记得自己搞过单个插入,单个删除,单个刷新,但是忘了怎么搞的了。
今天搞单个删除,结果各种异常,于是各种度娘,都没有个准确的说法。
所以写一个文章,记录一下,也分享一下。
这里不涉及业务,就以左滑删除为例子
// MARK: UITableViewDataSource
//这里是重点 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.modelArray.count;
}
// 每组一个数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//这里我习惯使用CustomTablevVewCell.description传入 Identifier
CustomTablevVewCel *cell = [tableView dequeueReusableCellWithIdentifier:CustomTablevVewCell.description];
if (!cell) {
cell = [[CustomTablevVewCel alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomTablevVewCell.description];
}
return cell
}
// 可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 左滑=删除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
// 左滑title
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"解绑";
}
//处理事件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//默认滑动直接删除
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"删除....");
}
}
第一个错误点,这里有坑,别直接复制代码
//处理事件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//默认滑动直接删除
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"删除....");
[self.modelArray removeObjectAtIndex:indexPath.section];
//这里直接调用了删除cell的函数
[self.listView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:self->_selectIndex]] withRowAnimation:UITableViewRowAnimationFade];
}
}
然后就抛异常了,大致理解就是数据源对不上
Invalid update: invalid number of sections. The number of sections contained
equal to the number of rows contained in that section before the update (1),
plus or minus the number of rows inserted or deleted from that section
(0 inserted, 1 deleted) and plus or minus the number of rows moved into or
out of that section (0 moved in, 0 moved out).
百度。。。。然后补代码
增加了 beginUpdates
与 endUpdates
,我模糊记得删除单个cell貌似不需要加,忘记了,有兴趣的可以自己试试😄
//处理事件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//默认滑动直接删除
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"删除....");
[self.modelArray removeObjectAtIndex:indexPath.section];
//这里直接调用了删除cell的函数
[tableView beginUpdates];
[self.listView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:self->_selectIndex]] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
然后继续抛异常,这时候我已经懵逼了,然后各种百度,发现是个普遍问题。其实这里有个注意点,就在数据源的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.modelArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
然后重点来了,关键位置
我这里是返回的组,所以就需要使用- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//默认滑动直接删除
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"删除....");
[self.modelArray removeObjectAtIndex:indexPath.section];
[tableView beginUpdates];
// [self.listView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
}
所以我需要删除的是section
而不是row
这里还有个注意点,划重点
当一个组内有多个数据,删除其中一条的时候还是要调用
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
当组内删除的只剩下一条数据的时候,必须调用
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
也就是只剩下一条数据的时候,删除的是组,而不是row数据
然后继续等待,iOS开放蓝牙5广播与WiFi 广播。