在点击不可选择的单元格之后,取消选择先前选择的UICollectionViewCells

Fab*_*iez 7 objective-c uikit ios uicollectionview uicollectionviewcell

我正在实现一个启用了多个选择的UICollectionView.

我的一些细胞是可选择的,有些则不是.这是一系列事件:

  1. 我通过点击它们并返回YES来 选择几个单元格
    • shouldHighlightItemAtIndexPath:
    • shouldSelectItemAtIndexPath:
  2. 我尝试通过点击它来选择一个不可选择的单元格(通过返回NO来实现不可选择的方面shouldSelectItemAtIndexPath:)
  3. 结果:取消选择所有选定的单元格并对其didDeselectItemAtIndexPath:进行调用.注意:shouldDeselectItemAtIndexPath:未调用.

预期结果:没有任何反应.

这是正常的行为吗?我在文档中找不到任何内容.如果是这样,我怎么能不去取消我的细胞呢?

小智 5

我不得不面对完全相同的问题,collectionView:shouldDeselectItemAtIndexPath:没有被召唤.我的解决方案包括手动重新选择当前选定的单元格,如果我点击一个不可选择的单元格:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL isSelectable = /* decide if currently tapped cell should be selectable */;

    NSIndexPath *selectedItemIndexPath = /* NSIndexPath of the current selected cell in the collection view, set in collectionView:didSelectItemAtIndexPath: */;

    if (!isSelectable) {
        // the cell isn't selectable, we have to reselect the previously selected cell that has lost selection in the meanwhile
        // without reloading first the cell the selection is not working...
        [collectionView reloadItemsAtIndexPaths:@[selectedItemIndexPath]];
        [collectionView selectItemAtIndexPath:selectedItemIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    }

    return isSelectable;
}
Run Code Online (Sandbox Code Playgroud)

如果您的集合视图正在滚动(隐藏当前选定的单元格),请记住重新选择该单元格collectionView:cellForItemAtIndexPath:.

我不太喜欢这个解决方案,它太"hacky",但它确实有效.我希望能做到所有的逻辑,collectionView:shouldDeselectItemAtIndexPath:但它没有被调用,我不明白为什么.


noo*_*lar 5

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeue...

    cell.userInteractionEnabled = isSelectableIndexPath(indexPath)

    return cell
}

func isSelectableIndexPath(indexPath: NSIndexPath) -> Bool {
    //logic to check if cell is selectable
}
Run Code Online (Sandbox Code Playgroud)

这可以通过禁用与​​单元格的交互来实现.