【发布时间】:2016-12-15 09:02:24
【问题描述】:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell
//let imageView = UIImageView()
//cell.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
cell.layer.borderWidth = 0.5
cell.layer.borderColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor
//cell.placeLabel.tintColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor
cell.layer.cornerRadius = 40
cell.layer.masksToBounds = true
print("places\(indexPath.row)")
//cell.placeLabel.text = places[indexPath.row] as! String
cell.placeLabel.text = places[indexPath.row]
cell.placeLabel.textColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
// code for VIew
let view = UIView(frame: cell.bounds)
//Set background color that you want
view.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
cell.selectedBackgroundView = view
return cell
我在收藏视图单元格中有一张图片。我只需要在单击单元格时显示该图像。我们必须在 cellforRowat 索引路径中执行吗?
//Custom class code:
@IBOutlet weak var tickImageView: UIImageView!
@IBOutlet weak var placeViewBtn: UIButton!
@IBOutlet weak var placeLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
override func layoutSubviews() {
}
override func awakeFromNib() {
self.imageView.layer.cornerRadius = self.imageView.frame.size.width/2
self.imageView.layer.masksToBounds = true
super.awakeFromNib()
// Initialization code
}
添加了自定义类代码。我想显示我在上面声明的 ticMark 图像。
【问题讨论】:
-
你必须使用 didselectrowatindexpath 方法
-
“我在集合视图单元格中有一张图片。我只需要在单击该单元格时显示该图片。”是什么意思?
-
@NiravD 实际上这是一个“tickmark”图像。当我单击collectionview 中的一个单元格时,该标记图像应仅显示在该单元格上。
-
@RakeshMohan 你设置了tableView的多选模式还是单选模式?
-
@RakeshMohan 为将被选中的特定单元格创建一个属性(可能是一个布尔变量)。 (这将在 didSelectItem 方法中更改)。重新加载您的 collectionview 单元格。在 cellForItemAtIndexPAth 方法中添加隐藏或显示 imageView 的逻辑。
标签: ios uicollectionview swift3