4

我想在锁定前n 个单元格时提供 UICollectionView 重新排序的选项。

为了表示前n 个单元格被“锁定”(无法重新排序),我想将它们的背景颜色更改为浅灰色。

到目前为止,我已经能够通过targetIndexPathForMoveFromItemAt

  override func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {

    // Gray out illegal moves
      for i in lockedIndex_start...lockedIndex_end {
        collectionView.cellForItem(at: IndexPath(item: i, section: 0))?.backgroundColor = UIColor.groupTableViewBackground
      }


    /* Check if user made an illegal move
     If Yes:
        Switch back
     Else:
        Update users personal order
     */

    if proposedIndexPath.item > lockedIndex_end {
      return proposedIndexPath
    }else{
      return originalIndexPath
    }
  }

我遇到的问题是检测用户何时释放重新排序单元格,以便我可以将锁定单元格的背景颜色设置为正常。

我尝试过使用moveItemAt,但是当用户激活重新排序但最终将原始单元格放回其原始索引或重新排序被取消时,它不会被调用。

我尝试过使用didUnhighlightItemAt,但该方法在 之后被调用targetIndexPathForMoveFromItemAt,而不是在用户释放单元格之后。

4

0 回答 0