【问题标题】:How to properly move array item when `UICollectionViewCell` position is changed更改“UICollectionViewCell”位置时如何正确移动数组项
【发布时间】:2018-03-05 15:49:49
【问题描述】:

我有一个UICollectionView,用户可以在查看每个 Swift 拖放页面后更改单元格的位置。我确定了一种能够更新字符串数组以跟踪此函数中的集合视图的方法:

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    let positionPreviouslyHoldingTheSelectedIndex = list[sourceIndexPath.item] 

    // put the select index in the intended destination index

    list[sourceIndexPath.item] = list[destinationIndexPath.item]


    // put the cell/index of the destination index in the selected positions old index

    list[destinationIndexPath.item] = positionPreviouslyHoldingTheSelectedIndex
}

但是上面的代码按预期更新了数组,它变得很奇怪,一旦调用moveItemAt,我将如何更新数组/列表。

【问题讨论】:

    标签: ios swift drag-and-drop uicollectionview


    【解决方案1】:

    有2个IndexPaths,你要把sourceIndexPath中的数组元素去掉,添加到destinationIndexPath,这样:

        let item = list.remove(at: sourceIndexPath.item)
        list.insert(item, at: destinationIndexPath.item)
    

    【讨论】:

    • 很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    相关资源
    最近更新 更多