我有一个UICollectionView,一切都运行正常,但是,有一件事我无法处理(我不知道怎么做),我有一组细胞,看到所有的细胞用户需要像往常一样向下或向上滚动。
当用户选择单元格时,内容更改为" red",红色是"选择"细胞,黑色是未选择的"细胞或正常状态。
当所选单元格落在navigationBar或TabBar后面时,单元格会丢失" red"再次变成黑色,就像#34;未选中"。
我如何保持"红色"当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
}
谢谢
答案 0 :(得分:7)
您需要对逻辑进行一些修改;
//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;
}
注意:更改图片图标颜色的方法并不好。它占用了太多的处理能力,可能会挂起滚动。每个州应该使用两个单独的图像。
答案 1 :(得分:0)
这种情况发生在我身上的UITableView,看起来每当你向上和向下滚动显示更多元素时,细胞再次呈现。所以我遇到的解决方案是创建一个名为selecteditems的数组,每次用户选择该Cell时,都会在数组中保存该Cell的索引。 像这样
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
}
}