單選功能
首先我們
_tableView.allowsMultipleSelectionDuringEditing = YES;
[_tableView setEditing:YES animated:YES];
然后在 didSelectRowAtIndexPath 選中數據 數據添加到數組中
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0];
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
lastCell.selected = YES;
_index = indexPath.row;
//afterDelay為延遲多少刪除上次的選中效果
[_tableView performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:lastIndex afterDelay:.0];
在 didDeselectRowAtIndexPath 從數組中移除數據
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0];
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
lastCell.selected = NO;
_index = -1;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
多選
多選相對簡單點 直接使用就行