【问题标题】:UICollectionView how to deselect allUICollectionView如何取消全选
【发布时间】:2016-11-20 16:53:35
【问题描述】:

我有一个带有集合视图的 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
    }
}

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

    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
}

在“选择”模式下,我可以对每个单元格进行选择,并且单元格上会显示一个复选标记。但是,我想要做的是有一个取消按钮来禁用所有选定的单元格并删除 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
    }
}

程序在这里崩溃给我一个致命错误:Unexpectedly found nil while unwrapping an optional error at

let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell

我不知道为什么将单元格展开为包含 checkImg 实例的 FollowCell 时遇到问题。我之前已经在 didSelectItemAtIndexPath 的类似情况下使用过它,它似乎可以工作?

谢谢,

【问题讨论】:

    标签: ios objective-c swift uicollectionview uicollectionviewcell


    【解决方案1】:

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

    您需要修改您的代码以处理潜在的nil 条件,但您也可以使用UICollectionViewindexPathsForSelectedItems 属性使您的代码更高效

     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
         }
     }
    

    【讨论】:

    • 谢谢。正是我需要和工作的。我不需要使用任何 reloadItems 代码。我按照你说的做了,效果和你说的完全一样。
    • 或者,如果您更喜欢 swifty:tableView.indexPathsForSelectedRows?.forEach({ tableView.deselectRow(at: $0, animated: false) })
    【解决方案2】:

    在 Swift 4 中使用扩展

    extension UICollectionView {
    
        func deselectAllItems(animated: Bool) {
            guard let selectedItems = indexPathsForSelectedItems else { return }
            for indexPath in selectedItems { deselectItem(at: indexPath, animated: animated) }
        }
    }
    

    【讨论】:

    • 太棒了,帮我在表格视图中实现了类似的功能。
    【解决方案3】:

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

    followCollectionView.allowsSelection = false
    followCollectionView.allowsSelection = true
    

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

    【讨论】:

      【解决方案4】:

      这个答案可能在 swift 4.2 中有用

       let selectedItems = followCollectionView.indexPathsForSelectedItems
      
       for (value in selectedItems) {
           followCollectionView.deselectItemAtIndexPath(value, animated:true)
           if let cell = followCollectionView.cellForItemAtIndexPath(value) as? FollowCell {
              cell.checkImg.hidden = true
           }
       }
      

      【讨论】:

        【解决方案5】:

        我这样做更容易解决:

         tableView.selectRow(at: nil, animated: true, scrollPosition: UITableView.ScrollPosition.top)
        

        【讨论】:

          【解决方案6】:
          collectionView.indexPathsForSelectedItems?
              .forEach { collectionView.deselectItem(at: $0, animated: false) }
          

          【讨论】:

            猜你喜欢
            • 2013-02-26
            • 1970-01-01
            • 2016-02-20
            • 2019-12-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多