當(dāng)程序出現(xiàn)這個提示的時候,是因為你一邊便利數(shù)組顶瞒,又同時修改這個數(shù)組里面的內(nèi)容敬尺,導(dǎo)致崩潰,錯誤的方法如下:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
AddSelectFuwushiCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selected = NO;
ResponseFuwushiListModel *model =_dataList[indexPath.row];
if (cell.selectBtn.selected) {
cell.selectBtn.selected = NO;
// NSMutableArray *tempArray = [[NSMutableArray alloc]initWithArray:_selectedArr];
for (ResponseFuwushiListModel *item in _selectedArr) {
if ([item.userId isEqualToString:model.userId]) {
[_selectedArr removeObject:item];
}
}
}else{
cell.selectBtn.selected = YES;
[_selectedArr addObject:model];
}
}
這種方法就是對數(shù)組_selectedArr 遍歷然后操作
今天終于找到了一個更快捷的刪除數(shù)組里面的內(nèi)容以及修改數(shù)組里面的內(nèi)容的方法:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
AddSelectFuwushiCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selected = NO;
ResponseFuwushiListModel *model =_dataList[indexPath.row];
if (cell.selectBtn.selected) {
cell.selectBtn.selected = NO;
[_selectedArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
ResponseFuwushiListModel *model1 = (ResponseFuwushiListModel*)obj;
if ([model1.userId isEqualToString:model.userId]) {
*stop = YES;
if (*stop == YES) {
[_selectedArr removeObject:model1];
}
}
if (*stop) {
NSLog(@"array is %@",_selectedArr);
}
}];
}else{
cell.selectBtn.selected = YES;
[_selectedArr addObject:model];
}
}
利用block來操作枉阵,發(fā)現(xiàn)block遍歷比for遍歷快20%左右,這個的原理是這樣的:
找到符合的條件之后预茄,暫停遍歷兴溜,然后修改數(shù)組的內(nèi)容
這種方法非常簡單有效