我有一个 UICollectionView
,用户可以在查看每个 Swift 拖放页面后更改单元格的位置。我确定了一种能够更新字符串数组并跟踪此函数中的 Collection View 的方法:
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
我将如何更新数组/列表。
最佳答案
有 2 个 IndexPath
,你必须从 sourceIndexPath
中去掉数组元素并将其添加到 destinationIndexPath
中,如下所示:
let item = list.remove(at: sourceIndexPath.item)
list.insert(item, at: destinationIndexPath.item)
关于ios - 如何在 `UICollectionViewCell` 位置更改时正确移动数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49114298/