UICollectionView如何取消选择所有

use*_*902 12 objective-c ios uicollectionview uicollectionviewcell swift

我有一个FollowVC和FollowCell设置与集合视图.我可以使用以下代码正确地将所有数据显示到我的uIcollection视图单元格中,没有任何问题.

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FollowCell", forIndexPath: indexPath) as? FollowCell {

        let post = posts[indexPath.row]

        cell.configureCell(post, img: img)

        if cell.selected == true {
            cell.checkImg.hidden = false
        } else {
            cell.checkImg.hidden = true
        }
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我还可以使用以下代码选择和取消选择多个图像

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    if deletePressed == true {
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
        cell.checkImg.hidden = false
    } else {
        let post = posts[indexPath.row]
        performSegueWithIdentifier(SEGUE_FOLLOW_TO_COMMENTVC, sender: post)
    }
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
    cell.checkImg.hidden = true
}
Run Code Online (Sandbox Code Playgroud)

在"选择"模式下,我可以执行每个单元格的选择,并在单元格上显示复选标记.但是,我想要做的是使用取消按钮来禁用所有选定的单元格并删除checkImg.

我试过了

    func clearSelection() {
    print("ClearSelection posts.count = \(posts.count)")

    for item in 0...posts.count - 1 {
        let indexP = NSIndexPath(forItem: item, inSection: 0)
        followCollectionView.deselectItemAtIndexPath(indexP, animated: true)
        let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
        cell.checkImg.hidden = true
    }
}
Run Code Online (Sandbox Code Playgroud)

程序崩溃在这里给我一个致命的错误:在解开可选错误的时候意外地发现了nil

let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
Run Code Online (Sandbox Code Playgroud)

我不知道为什么在将单元格解包为包含checkImg实例的FollowCell时遇到问题.我之前在didSelectItemAtIndexPath中的类似情况中已经使用过它,它似乎有效吗?

谢谢,

Pau*_*w11 25

当您清除选择状态时,并非所有选定的单元格都在屏幕上,因此collectionView.cellForItemAtIndexPath(indexPath)可能返回nil.由于你有一个强制转发,在这种情况下你会得到一个例外.

您需要修改代码以处理潜在的nil条件,但您也可以使用indexPathsForSelectedItems属性来提高代码的效率UICollectionView

 let selectedItems = followCollectionView.indexPathsForSelectedItems
 for (indexPath in selectedItems) {
     followCollectionView.deselectItemAtIndexPath(indexPath, animated:true)
     if let cell = followCollectionView.cellForItemAtIndexPath(indexPath) as? FollowCell {
        cell.checkImg.hidden = true
     }
 }
Run Code Online (Sandbox Code Playgroud)

  • 或者,如果您更喜欢它:`tableView.indexPathsForSelectedRows?.forEach({tableView.deselectRow(at:$ 0,animation:false)}) (2认同)

小智 6

在Swift 4中使用扩展

extension UICollectionView {

    func deselectAllItems(animated: Bool) {
        guard let selectedItems = indexPathsForSelectedItems else { return }
        for indexPath in selectedItems { deselectItem(at: indexPath, animated: animated) }
    }
}
Run Code Online (Sandbox Code Playgroud)


Teo*_*adu 5

为了进一步简化,你可以这样做

followCollectionView.allowsSelection = false
followCollectionView.allowsSelection = true
Run Code Online (Sandbox Code Playgroud)

事实上,这将正确清除您的 followCollectionView.indexPathsForSelectedItems 即使感觉非常错误。