![](/img/trans.png)
[英]UICollectionView: glitches while scrolling to the last cell with animation after cell deletion
[英]UICollectionView and Selected Cell loses selection while scrolling
我有一個UICollectionView,一切都工作正常,但是,有一件事我無法處理(我不知道如何),我有一個單元格的集合,以查看用戶需要向下或向上滾動的所有單元格像往常一樣。
當用戶選擇單元格時,內容變為“紅色”,紅色是“選中”單元格,黑色是“未選定”單元格或正常狀態。
當所選單元格落在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
}
謝謝
您需要對邏輯進行一些修改;
//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;
}
注意:更改圖像圖標顏色的方法並不好。 它占用了太多的處理能力,可能會掛起滾動。 每個州應該使用兩個單獨的圖像。
這種情況發生在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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.