【发布时间】:2017-04-08 06:14:57
【问题描述】:
我需要联系我的收藏视图中的一个具体单元格不点击它。例如,我需要更改第二个单元格中图像的颜色并将其变为黑色。你知道有什么解决办法吗?
这是一张图片:
这是我的代码:
import UIKit
class SimplestCollectionViewCell: UICollectionViewCell {
var sImageView: UIImageView!
override func awakeFromNib() {
sImageView = UIImageView(frame: contentView.frame)
sImageView.contentMode = .scaleToFill
sImageView.clipsToBounds = true
let sImage = UIImage(named: "banana.png")?.withRenderingMode(.alwaysTemplate)
sImageView.image = sImage
sImageView.tintColor = UIColor.orange
contentView.addSubview(sImageView)
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 49
}
// we use this method to dequeue the cell and set it up
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sCell", for: indexPath) as! SimplestCollectionViewCell
cell.awakeFromNib()
return cell
}
}
【问题讨论】:
-
你想要备用背景颜色单元格吗?
-
不,我只是想将第二个单元格中的香蕉颜色更改为黑色,例如。
-
为什么不采用一个简单的标志来决定单元格是否需要将背景设置为黑色,在您的 cellForItemAt indexPath 中设置标志值。
-
谢谢,但我以前从未见过,抱歉。可以给个样品怎么做吗?
-
@Roman 当然我会添加一个相同的答案
标签: swift uicollectionview uicollectionviewcell