/** TableView 进入或退出编辑状态(TableView 方法). */
- (void)setEditing:(BOOL)editing animated:(BOOL)animate{
/*首先调用父类的方法*/
[super setEditing:editing animated:animated];
/*使tableView出于编辑状态*/
[self.tableView setEditing:editing animated:animated];
}
/** 确定哪些行的cell可以编辑 (UITableViewDataSource协议中方法). */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if (0 == indexPath.row) {
return No; /*第一行不能进行编辑*/
} else {
return Yes;
}
}
/** 设置某一行cell的编辑模式 (UITableViewDelegate协议中方法). */
- (TableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row > 10){
return UITableViewCellEditingStyleInsert;
}else{
return
UITableViewCellEditingStyleDelete;
}
}
/** 提交编辑状态 (UITableViewDataSource协议中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
/** 点击 删除 按钮的操作 */
if (editingStyle == UITableViewCellEditingStyleDelete) {
/**< 判断编辑状态是删除时. */ /** 1. 更新数据源(数组): 根据indexPaht.row作为数组下标, 从数组中删除数据. */
[self.arr removeObjectAtIndex:indexPath.row];
/** 2. TableView中 删除一个cell. */
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
/** 点击 +号 图标的操作. */
if (editingStyle == UITableViewCellEditingStyleInsert) {
/**< 判断编辑状态是插入时. */ /** 1. 更新数据源:向数组中添加数据. */
[self.arr insertObject:@"abcd" atIndex:indexPath.row]; /** 2. TableView中插入一个cell. */
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
/** 提交编辑状态 (UITableViewDataSource协议中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
/** 插入 cell (UITableView 方法). */
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
/** 删除 cell (UITableView 方法). */
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
上面的需求思路:
1.tableView 要进入可编辑状态(根据自己的需要)
2.更具自己需要,删除,或者,插入,指定Cell
3.最后对数据进行操作,刷新页面