好久以前記得自己搞過(guò)單個(gè)插入,單個(gè)刪除,單個(gè)刷新,但是忘了怎么搞的了。
今天搞單個(gè)刪除,結(jié)果各種異常,于是各種度娘,都沒(méi)有個(gè)準(zhǔn)確的說(shuō)法。
所以寫一個(gè)文章,記錄一下,也分享一下。
這里不涉及業(yè)務(wù),就以左滑刪除為例子
// MARK: UITableViewDataSource
//這里是重點(diǎn) 返回組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.modelArray.count;
}
// 每組一個(gè)數(shù)據(jù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//這里我習(xí)慣使用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{
//默認(rèn)滑動(dòng)直接刪除
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"刪除....");
}
}
第一個(gè)錯(cuò)誤點(diǎn),這里有坑,別直接復(fù)制代碼
//處理事件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//默認(rèn)滑動(dòng)直接刪除
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"刪除....");
[self.modelArray removeObjectAtIndex:indexPath.section];
//這里直接調(diào)用了刪除cell的函數(shù)
[self.listView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:self->_selectIndex]] withRowAnimation:UITableViewRowAnimationFade];
}
}
然后就拋異常了,大致理解就是數(shù)據(jù)源對(duì)不上
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).
百度。。。。然后補(bǔ)代碼
增加了 beginUpdates 與 endUpdates,我模糊記得刪除單個(gè)cell貌似不需要加,忘記了,有興趣的可以自己試試??
//處理事件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//默認(rèn)滑動(dòng)直接刪除
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"刪除....");
[self.modelArray removeObjectAtIndex:indexPath.section];
//這里直接調(diào)用了刪除cell的函數(shù)
[tableView beginUpdates];
[self.listView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:self->_selectIndex]] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
然后繼續(xù)拋異常,這時(shí)候我已經(jīng)懵逼了,然后各種百度,發(fā)現(xiàn)是個(gè)普遍問(wèn)題。其實(shí)這里有個(gè)注意點(diǎn),就在數(shù)據(jù)源的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.modelArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
然后重點(diǎn)來(lái)了,關(guān)鍵位置
我這里是返回的組,所以就需要使用- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//默認(rèn)滑動(dòng)直接刪除
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];
}
}
所以我需要?jiǎng)h除的是section而不是row
這里還有個(gè)注意點(diǎn),劃重點(diǎn)
當(dāng)一個(gè)組內(nèi)有多個(gè)數(shù)據(jù),刪除其中一條的時(shí)候還是要調(diào)用
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
當(dāng)組內(nèi)刪除的只剩下一條數(shù)據(jù)的時(shí)候,必須調(diào)用
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
也就是只剩下一條數(shù)據(jù)的時(shí)候,刪除的是組,而不是row數(shù)據(jù)
然后繼續(xù)等待,iOS開(kāi)放藍(lán)牙5廣播與WiFi 廣播。