当启用allowmultiesselection时,如何在UICollectionView中以编程方式取消单元格的选择



我在集合视图中启用了allowmultiplesselection。细胞从它们的点击时选择的状态。所有的好。但是,当我想使用下面的代码将整个视图重置为selected state:NO时,单元格似乎完全被取消选中,直到我进行新的选择,此时所有先前选择的单元格显示其先前选择的状态。

。尽管外观collectionview没有更新它的当前选择列表时,我通过编程取消选择单元格

- (void)clearCellSelections {
   for (LetterCell  *cell in self.collectionView.visibleCells) {
        [cell prepareForReuse];
    }
}

自定义单元格:

- (void)prepareForReuse
{
    [super prepareForReuse];
    [self setSelected:NO];
}

我做错了什么?是否有其他方法取消选择所有单元格?

Thanks TBlue for a look

可以遍历- [UICollectionView indexPathsForSelectedItems]:

for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

取消选择UICollectionView中所有选定单元格的最简单方法是将nil作为第一个参数传递给collectionView.selectItem(at:, animated:, scrollPosition:)。例如,

collectionView.selectItem(at: nil, animated: true, scrollPosition: [])

将清除当前选择状态,即使allowsMultipleSelection == true .

您可以说UITableViewCell.selected只设置单元格及其内容的"可见状态/外观"。你可以取消选择单元格,通过迭代tableView的所有indexPaths并为每个调用deselectRowAtIndexPath:animated:

例如:

for (int i=0; i < self.myData.count; i++) {
    [self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:YES];
}

EDIT:我完全同意@BenLings和@JeremyWiebe的评论,@qorkfiend的解决方案比这个更好。

如果这是Swift的简单解决方案:

extension UICollectionView {
    func deselectAllItems(animated animated: Bool = false) {
        for indexPath in self.indexPathsForSelectedItems() ?? [] {
            self.deselectItemAtIndexPath(indexPath, animated: animated)
        }
    }
}

对于swift 3扩展将看起来像这样:

import UIKit
extension UICollectionView {
    func deselectAllItems(animated: Bool = false) {
        for indexPath in self.indexPathsForSelectedItems ?? [] {
            self.deselectItem(at: indexPath, animated: animated)
        }
    }
}

如果您想委托也称为

,则可以完成此操作。
for (NSIndexPath *indexPath in [self.cuisineCollection indexPathsForSelectedItems]) {
            [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
            [collectionView.delegate collectionView:cuisineCollection didDeselectItemAtIndexPath:indexPath];
        }

我创建了一个名为toggleCellSelection的全局变量,然后在didSelectItemAt函数中运行它:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("select cell (indexPath.row)")
    let cell = collectionView.cellForItem(at: indexPath)
    if (toggleCellSelection == true) {
        toggleCellSelection = false
        cell?.layer.borderWidth = 0
        cell?.layer.borderColor = UIColor.clear().cgColor
    } else {
        toggleCellSelection = true
        cell?.layer.borderWidth = 5
        cell?.layer.borderColor = #colorLiteral(red: 0.8779790998, green: 0.3812967837, blue: 0.5770481825, alpha: 1).cgColor
    }

}

这个答案不一定是"最好的",但既然没有人提到它,我就把它加进去。

你可以简单地调用下面的代码。

collectionView.allowsSelection = false
collectionView.allowsSelection = true

这是@qorkfiend在Swift中的答案

// this is an array of the selected item(s) indexPaths
guard let indexPaths = collectionView.indexPathsForSelectedItems else { return }
// loop through the array and individually deselect each item
for indexPath in indexPaths{
    collectionView.deselectItem(at: indexPath, animated: true)
}

最新更新