UITableView移动

移动的步骤:

与编辑的第一步一样

#pragma mark 响应方法

- (void)leftAction:(UIBarButtonItem *)sender{

    NSLog(@"添加..");

    // 记录添加样式

    _editingStyle = UITableViewCellEditingStyleInsert;

   

    // 1.让tableView处于编辑状态

    // 加动画

    [self.rv.tabelView setEditing:!self.rv.tabelView.editing animated:YES];

   

   

}

指定哪些行可以移动

// 2.指定哪些行可以移动(第一步与添加删除一样,处于编辑状态)

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

这里return YES的意思是所有行都可以移动

完成移动

// 3.完成移动

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

   

    // 修改数据源

    // 取原来的

//    NSString *str = self.dataArr[sourceIndexPath.section][sourceIndexPath.row];

//   

//    self.dataArr[sourceIndexPath.section][sourceIndexPath.row] = self.dataArr[destinationIndexPath.section][destinationIndexPath.row];

//    self.dataArr[destinationIndexPath.section][destinationIndexPath.row] = str;

   

    // 思路:删除原来的,插入新的

    // 把原来的东西取出来放在str里

//    NSLog(@"%@",self.dataArr[sourceIndexPath.section][sourceIndexPath.row]);

    NSString *str = self.dataArr[sourceIndexPath.section][sourceIndexPath.row] ;

    // 删除原来的

    [self.dataArr[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];

    // 把str 插入新位置

    [self.dataArr[destinationIndexPath.section] insertObject:str atIndex:destinationIndexPath.row];

   

    // 修改UI

    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

}

解释;

1、移动与编辑的区别,就是编辑第三步要给定一个样式,而移动不需要给定样式。

2、修改数据源:要把原来位置的数据,放在一个临时变量里,然后删除原来位置的数据,再在新的目标位置插入这个数据内容。

(1)、把原来位置的数据取出来放在临时变量str里。NSString *str = self.dataArr[sourceIndexPath.section][sourceIndexPath.row]

这里sourceIndexPath.section事原来位置的分区,sourceIndexPath.row原来位置分区里的第几行。(因为数据结构是大数组嵌套两个小数组,sourceIndexPath.section来去到小数组,sourceIndexPath.row来去到小数组里的元素)

(2)、删除原来位置值[self.dataArr[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row]。

(3)、把原来位置的数据插入新的位置

[self.dataArr[destinationIndexPath.section] insertObject:str atIndex:destinationIndexPath.row]

3、修改UI:[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]。

第一个参数:原来位置

第二个位置:移到的目标位置

4、但是这样的移动,会跨分区移动,会导致系统崩溃,因此,在移动的时候,需要对移动的位置进行监测:

#pragma mark TableView 自己的方法

 

 // 监视移动是否跨区域

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{

    // 判断是否再同一个区域,再同一区域,返回目的分区的行,不再同一区域,返回原来分区的行

    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {

        return proposedDestinationIndexPath;

    }else{

        return sourceIndexPath;

    }

}

解释:

1、

第一个参数:当前操作的tableView

第二个参数:原来位置

第三个参数:目标位置

2、判断,如果原来位置的分区等于目标位置的分区。返回新的位置,否则不在同一个分区,返回原来的位置。

注意,因为以上这些方法,都是RootViewController遵循了UITableViewDataSource,UITableViewDelegate这两个协议,协议里的方法,所以,必须要按照代理的方法,设置代理等步骤

设置代理:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.rv.tabelView.dataSource = self;

    self.rv.tabelView.delegate = self;

    [self p_data];

    // 处理数据

    // test

//    self.rv.tabelView.editing = YES;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction:)];

   

}

原文地址:https://www.cnblogs.com/Coder-GT/p/4886153.html