简体   繁体   English

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

[英]UICollectionView and Selected Cell loses selection while scrolling

i have an UICollectionView, everything is working fine, but, there is a thing that i can't handle (i don't know how), i have a collection of cells, to see all cells the user needs to scroll down or up like always. 我有一个UICollectionView,一切都工作正常,但是,有一件事我无法处理(我不知道如何),我有一个单元格的集合,以查看用户需要向下或向上滚动的所有单元格像往常一样。

在此输入图像描述

When the user select the cell, the content change to "red", red is the "selected" cell, black is the "unselected" cell or normal state. 当用户选择单元格时,内容变为“红色”,红色是“选中”单元格,黑色是“未选定”单元格或正常状态。

When the selected cells fall behind the navigationBar or the TabBar, the cell loses the "red" and become black again, like "unselected". 当所选单元格落在navigationBar或TabBar后面时,单元格将丢失“红色”并再次变为黑色,如“未选中”。

How i can keep the "red" when the cell fall behind while the uicollectionview is scrolling? 当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
}

Thank you 谢谢

You will need to do some modifications in your logic; 您需要对逻辑进行一些修改;

//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.

}

and then; 然后;

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

Note: The approach of changing image icon color is not good. 注意:更改图像图标颜色的方法并不好。 it is taking too much processing power which may hang the scrolling. 它占用了太多的处理能力,可能会挂起滚动。 There should be used two separate images for each state. 每个州应该使用两个单独的图像。

This happen to me with the UITableView, it seems like everytime you scroll up and down showing more elements, the cell renders again. 这种情况发生在UITableView上,看起来每当你上下滚动显示更多元素时,细胞再次呈现。 So the solution I came across was to create a Array called selecteditems, and everytime the user select that Cell, saved that index of that Cell on the array. 所以我遇到的解决方案是创建一个名为selecteditems的数组,每次用户选择该Cell时,都会在数组中保存该Cell的索引。 like this 像这样

  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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM