UITableView使用系统编辑移动位置moveRowAtIn
2023-03-28 本文已影响0人
微步毂纹生
UITableView使用系统默认编辑模式做移动位置操作,移动后需要通过moveRowAtIndexPath回调对数组进行操作,一开始使用exchangeObjectAtIndex后发现其更换位置逻辑与moveRowAtIndexPath不一致,所以需要自己对数字进行重新排序。
代码如下
参考:https://stackoverflow.com/questions/28637660
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//sourceIndexPath 原本位置
//destinationIndexPath移动后位置
if(sourceIndexPath != destinationIndexPath){
NSObject *object = self.dataArray[sourceIndexPath.row];
[self.dataArray removeObjectAtIndex:sourceIndexPath.row];
[self.dataArray insertObject:object atIndex:destinationIndexPath.row];
}
}