Cell关联Block不可传值indexPath进行deleteRows操作,当传入index用此deleteRows会index没有更新但是数组却在变化所以会溢出,可使用reloadData更新,因为所有的index都会更新.————————>deleteRow使用的时候单次删除一行会进行多次删除的时候不应该使用index path操作,也是不能用cell绑定index属性。
ture:
__weakCooperaterShowViewController *weakSelf=self;
showCell.ocShow=^(NSString *num,NSIndexPath *index){
SwitchOptionalModel *switchModel=weakSelf.variousChange[index.row];
switchModel.num=num;
if (num.integerValue<=0) {
[weakSelf.variousChange removeObjectAtIndex:index.row];
[weakSelf.cooperaterShowTable reloadData];
}
};
- (IBAction)reduceAction:(id)sender {
NSInteger num=_selectedNum.text.integerValue-1;
_selectedNum.text=[NSString stringWithFormat:@"%ld",num];
[[NSNotificationCenter defaultCenter]postNotificationName:@"CooperKeepShow" object:nil userInfo:@{@"index":_indexPath,@"num":@(num)}];
}
- (IBAction)addAction:(id)sender {
NSInteger num=_selectedNum.text.integerValue+1;
_selectedNum.text=[NSString stringWithFormat:@"%ld",num];
[[NSNotificationCenter defaultCenter]postNotificationName:@"CooperKeepShow" object:nil userInfo:@{@"index":_indexPath,@"num":@(num)}];
}
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keepShowCooperNomal:) name:@"CooperKeepShow" object:nil];
-(void)keepShowCooperNomal:(NSNotification *)noti{
NSDictionary *infoDic=noti.userInfo;
NSNumber *num=infoDic[@"num"];
NSIndexPath *index=infoDic[@"index"];
//通过传过来的index拿到原始标记的indexPath,匹配传值进入model里面indexPath,就可以拿到即使删除了也能确定位置的model和row的确切位置。
for (NSInteger i=0; i<_variousChange.count; i++) {
SwitchOptionalModel *model=_variousChange[i];
if (model.indexInShow.row ==index.row) {
model.num=[NSString stringWithFormat:@"%ld",num.integerValue];
if (!num.integerValue) {
[_cooperaterShowTable beginUpdates];
[_variousChange removeObject:model];
[_cooperaterShowTable deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[_cooperaterShowTable endUpdates];
if (!_variousChange.count) {
if (self.cooperaterShowChange) {
self.cooperaterShowChange(YES,_noShowPoint);
}
[UIView animateWithDuration:.3 animations:^{
self.view.alpha=0;
}];
}
}
return;
}
}
}