美文网首页
UITableView使用系统编辑移动位置moveRowAtIn

UITableView使用系统编辑移动位置moveRowAtIn

作者: 微步毂纹生 | 来源:发表于2023-03-28 15:04 被阅读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];
    }
}

相关文章

网友评论

      本文标题:UITableView使用系统编辑移动位置moveRowAtIn

      本文链接:https://www.haomeiwen.com/subject/uhxxddtx.html