UICollectionView如何取消全选 [英] UICollectionView how to deselect all

查看:468
本文介绍了UICollectionView如何取消全选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有集合视图的FollowVC和FollowCell设置.我可以使用以下代码将所有数据正确显示在uIcollection视图单元中,而不会出现任何问题.

I have a FollowVC and FollowCell Setup with collection View. I can display all the datas correctly into my uIcollection view cell using the following code with no problem.

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

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

Note that I could also select and deselect multiple images using the following code

    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.

When In "Select" mode, I can perform the selction of each cell and a check mark will be displayed on the cell. However, what I want to do is to have a cancel buttom to disable all the selected cell and removing the 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
    }
}

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

The program crashes here giving me a fatal error: Unexpectedly found nil while unwrapping an optional error at

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

我不知道为什么要在将单元格分解为我的FollowCell(其中包含checkImg实例)时遇到麻烦.我以前曾在didSelectItemAtIndexPath中以类似情况使用过它,它似乎可以正常工作吗?

I dont know why it is having trouble unwrapping the cell to be my FollowCell which contains an instance of the checkImg. I already used it before in a similar situation in didSelectItemAtIndexPath and it seems to work?

谢谢

推荐答案

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

Not all of the selected cells may be on screen at the point when you are clearing the selection status, so collectionView.cellForItemAtIndexPath(indexPath) may return nil. Since you have a force downcast you will get an exception in this case.

您需要修改代码以处理潜在的nil条件,但也可以通过使用UICollectionView

You need to modify your code to handle the potential nil condition but you can also make your code more efficient by using the indexPathsForSelectedItems property of 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
     }
 }

这篇关于UICollectionView如何取消全选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆