一.pragma mark ——————– tableView的编辑
tableView的四个步骤:
1.开启tableView的编辑状态:我们需要的是table的可编辑状态
[self.tableView setEditing:!self.tableView.editing animated:YES];
2.允许哪个分区的哪行是可以编辑的(默认都是能编辑的)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{ // 也可以利用indexPath 控制那个分区的那个行 不能编辑
return YES;
}
3.指定可编辑的样式
- (UITabelViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 返回编辑的样式:
// 根据需求 第几个section区 第几行row 给变编辑样式
return UITableViewCellEditingStyleInsert; // 添加样式
return UTableViewCellEditingStyleDelete; // 删除样式(默认的样式)
}
4.提交编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath)indexPath
{
if (indexPath.section == 0)
{ // 分区1
if (editingStyle == UTableViewCellEditingStyleDelete)
{ // 删除样式:
// 先删除数据
[ self.firstArray removeObjectAtIndex:indexPath.row];
// 再刷新UI
// 整体刷新(刷新的是tableView) 重新走一遍数据源代理方法 达到刷新页面的方法 (常用方法)
// [tableView reloadData];
// 删除的刷新方法
// 该方法可以进行多行删除,数组中填所有已经删除的索引
tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// 添加样式
// 先操作数据
[self.firstArray insertObject:@"123" atIndex:indexPath.row];
// 刷新页面
//[tableView reloadData];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
}
// section = 1 的情况下: 操作和上面的一致
}
}
二.pragma mark ——————– tableView的移动
移动的4步骤:
1.开启tableView的可以编辑状态
[tableView setEditing : !tableView.Editing animated:YES];
2.允许哪个分区的哪一行 可以编辑canMove
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
3.完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// sourceIndexPath 来源的索引
// destinationIndexPath 目的地的索引
// 移动的分类 同分区移动 和 跨分区移动
// 我们不考虑跨区移动,反而要监控是否跨区移动
// 判断:分区的移动
if (sourceIndexPath.section == destinationIndexPath.
section) {
// 同区:
if (sourceIndexPath.section == 0) {
// 操作firstArray (移动的关键在于:先删除你要移动的元素,再插入到你要插入到的位置)
// 把来源索引下 数组对应的元素 保存一下
NSString *temp = self.firstArray[sourceIndexPath.row];
// 用来源的索引 删除数组中对应元素
[self.firstArray removeObjectAtIndex:sourceIndexPath.row];
// 然后 再插入到目的地索引处
[self.firstArray insertObject:temp atIndex:destinationIndexPath.row];
}else
{
// 操作SecondArraay
// 步骤同上:
// 先接受一下来源索引处的元素
NSString *temp = self.secondArray[sourceIndexPath.row];
// 删除来源索引对应的数组中对应的元素
[self.secondArray removeObjectAtIndex:sourceIndexPath.row];
// 插入到目的地索引处
[self.secondArray insertObject:temp atIndex:destinationIndexPath.row];
}
}
// 刷新UI界面 UI移动专用刷新方法
[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
4.限制跨区移动
// 你只要一移动就执行监测有没有跨区
- (NSIndexPath *)tableview:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposeIndexPath:(NSIndexPath *)proposeDrstinationIndexPath
{
// sourceIndexPath:来源索引
// proposedDestinationIndexPath:建议的目的地索引
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
// 同区移动 允许指向
return proposedDestinationIndexPath;
}else
// 跨区移动 需要限制 (从哪来回哪去)
return sourceIndexPath;
}