UICollectionView 和 Selected Cell 在滚动时失去选择

Posted

技术标签:

【中文标题】UICollectionView 和 Selected Cell 在滚动时失去选择【英文标题】:UICollectionView and Selected Cell loses selection while scrolling 【发布时间】:2015-10-25 05:05:56 【问题描述】:

我有一个 UICollectionView,一切正常,但是,有一件事情我无法处理(我不知道如何处理),我有一个单元格集合,可以查看用户需要滚动的所有单元格像往常一样向下或向上。

当用户选择单元格时,内容变为“红色”,红色为“选中”单元格,黑色为“未选中”单元格或正常状态。

当所选单元格落在导航栏或标签栏后面时,该单元将失去“红色”并再次变为黑色,如“未选择”。

如何在 uicollectionview 滚动时单元格落后时保持“红色”?

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
    let cell = collectionView.cellForItemAtIndexPath(indexPath)

    let icon = cell!.viewWithTag(10) as? UIImageView
    icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    icon!.tintColor = UIColor.redColor()

    let label = cell!.viewWithTag(100) as? UILabel
    label?.textColor = UIColor.redColor()
    //print("Seleccionado")



override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) 
    let cell = collectionView.cellForItemAtIndexPath(indexPath)

    cell!.contentView.backgroundColor = nil
    cell!.contentView.layer.borderColor = nil

    let icon = cell!.viewWithTag(10) as? UIImageView
    icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    icon!.tintColor = UIColor.blackColor()

    let label = cell!.viewWithTag(100) as? UILabel
    label?.textColor = UIColor.lightGrayColor()
    //print("Unselect")


override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ObjectosCollectionViewCell

    cell.objectoNameLabel.text = objectosData[indexPath.row]
    cell.objectoNameLabel.textColor = UIColor.lightGrayColor()
    cell.objectoImageView.image = UIImage(named:objectosImage[indexPath.row])

    return cell

谢谢

【问题讨论】:

【参考方案1】:

你需要对你的逻辑做一些修改;

//CollectionViewCell Custom Class
import UIKit

class CollectionViewCell: UICollectionViewCell 

    override var selected: Bool 
        get 
            return super.selected;
        

        set 
            if (super.selected != newValue) 
                super.selected = newValue

                let icon = self.viewWithTag(10) as? UIImageView
                icon?.image = icon?.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)

                let label = self.viewWithTag(100) as? UILabel

                if (newValue == true) 
                    icon?.tintColor = UIColor.redColor()
                    label?.textColor = UIColor.redColor()
                 else 
                    icon?.tintColor = UIColor.blackColor()
                    label?.textColor = UIColor.lightGrayColor()
                
            
        
     //P.E.


然后;

//Define a class variable in your viewController
var cellStatus:NSMutableDictionary = NSMutableDictionary();

//Collection view delegate methods
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 

    var cell:CollectionViewCell? = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CollectionViewCell;

    cell!.selected = (cellStatus[indexPath.row] as? Bool) ?? false;

    return cell!;


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
    //Updating cell status
    let cell = collectionView.cellForItemAtIndexPath(indexPath)
    cell?.selected = true;

    //Updating dic
    self.cellStatus[indexPath.row] = true;


func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) 
    //Updating cell status
    let cell = collectionView.cellForItemAtIndexPath(indexPath)
    cell?.selected = false;

    //Updating dic
    self.cellStatus[indexPath.row] = false;

注意:改变图片图标颜色的方法不好。它占用了太多的处理能力,可能会挂起滚动。每个状态应该使用两个单独的图像。

【讨论】:

谢谢,我喜欢这个解决方案,我试过了,但一切又变回黑色了 在实施我建议的解决方案时,您是否删除了将图像和标签颜色更改为黑色的代码? 我删除了它,因为我在 CollectionViewCell 中看到了相同的代码 代码运行良好。你一定错过了什么。尝试找出问题所在。供参考,您可以在此处查看示例代码files.fm/u/vofdysq【参考方案2】:

这发生在我的 UITableView 上,似乎每次您上下滚动显示更多元素时,单元格都会再次呈现。所以我遇到的解决方案是创建一个名为 selecteditems 的数组,每次用户选择该单元格时,都会将该单元格的索引保存在数组中。 像这样

  override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
let cell = collectionView.cellForItemAtIndexPath(indexPath)

let icon = cell!.viewWithTag(10) as? UIImageView
icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
icon!.tintColor = UIColor.redColor()

let label = cell!.viewWithTag(100) as? UILabel
label?.textColor = UIColor.redColor()

//Save in array
selecteditems.append(indexPath.row)

override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) 
    let cell = collectionView.cellForItemAtIndexPath(indexPath)

    cell!.contentView.backgroundColor = nil
    cell!.contentView.layer.borderColor = nil

    let icon = cell!.viewWithTag(10) as? UIImageView
    icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    icon!.tintColor = UIColor.blackColor()

    let label = cell!.viewWithTag(100) as? UILabel
    label?.textColor = UIColor.lightGrayColor()


    if selecteditems.contains(indexPath.row)
      // place your code to be red
    


【讨论】:

谢谢,我试过了,但没有任何变化,确定我的元素有数组,但一切又变黑了

以上是关于UICollectionView 和 Selected Cell 在滚动时失去选择的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionView 中的垂直和水平滚动方向

为 UICollectionView 指定行号和列号

UICollectionView:在标题和项目之间添加间距

UICollectionView:contentSize和bounds有啥区别?

UICollectionView - 分离 scrollViewDidScroll 和 scrollToItemAtIndexPath

IOS---UICOLLECTIONVIEW详解和常用API翻译