UICollectionView在滚动时选择的单元格会失去选择

4
我有一个UICollectionView,一切都运行正常,但有一件事我无法处理(不知道如何解决),我有一组单元格,要查看所有单元格,用户需要像往常一样向下或向上滚动。

enter image description here

当用户选择单元格时,内容会变为“红色”,红色是“选定”单元格,黑色是“未选定”单元格或正常状态。
当选定的单元格落后于navigationBar或TabBar时,该单元格将失去“红色”并再次变为黑色,就像“未选定”一样。
我如何在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
}

谢谢

2个回答

8

您需要在逻辑上进行一些修改;

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

注意: 改变图像图标颜色的方法不是最好的。它会消耗太多的处理能力,可能会影响滚动。应该使用两张不同状态的图片。


谢谢,我喜欢这个解决方案,我尝试了,但是一切又回到了黑色。 - Asinox
1
在实施我的建议解决方案时,您是否删除了更改图像和标签颜色为黑色的代码? - Shoaib
我将其删除了,因为我在CollectionViewCell中看到了相同的代码。 - Asinox
1
代码运行良好。你可能漏掉了什么。尝试找出问题所在。参考示例代码,请访问此处http://files.fm/u/vofdysq。 - Shoaib

0
这种情况在我使用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
    }

}

谢谢,我尝试了这个,但是没有任何改变,我确定我的元素有一个数组,但是一切又变成黑色了。 - Asinox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接