1)进入编辑状态
//调用一个方法,让cell进入编辑状态进入编辑状态
- (void)changeSegmentedC:(UISegmentedControl *)sender{
[self.tableView setEditing:sender.selectedSegmentIndex animated:YES];
return;
}
2)设置每个cell进入什么样的编辑状态
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == self.dataArr.count - 1) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
3)设置哪些cell可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return NO;
}
return YES;
}
4)自定义删除按钮的title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *str = [NSString stringWithFormat:@"册除%@",self.dataArr[indexPath.row]];
return str;
}
5)松手后取消选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
6)添加和删除cell
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
switch (editingStyle) {
case UITableViewCellEditingStyleDelete:
[self.dataArr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case UITableViewCellEditingStyleInsert:{
[self.dataArr addObject:@"学习"];
NSIndexPath *indexP = [NSIndexPath indexPathForRow:self.dataArr.count - 1 inSection:0];
[tableView insertRowsAtIndexPaths:@[indexP] withRowAnimation:UITableViewRowAnimationLeft];
}
default:
break;
}
}