我有一個UICollectionView,一切工作正常,但是,有一件事我無法處理(我不知道如何),我有一個單元集合,以查看用戶需要向下滾動或向上滾動的所有單元格。UICollectionView和Selected Cell在滾動時失去選擇
當用戶選擇單元格,內容改變爲「紅」,紅色是「選擇的」細胞,黑色是「未選擇的」細胞或正常狀態。
當選定單元格落在導航欄或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
}
謝謝
謝謝,我喜歡這個解決方案,我試過了,但是一切都會再次變回黑色 – Asinox
你能顯示cellForItemAtIndexPath的代碼嗎? – Shoaib
當然,編輯,添加 – Asinox