【发布时间】:2019-02-16 22:33:41
【问题描述】:
我有一个带有一系列自定义类单元格的 uicollectionview,其中包含一些文本视图和一个 uibutton。有超过 100 个单元格,我只想切换每个单元格的 uibutton 图像。 uibutton 是一个收藏按钮,与大多数应用程序一样,我只想收藏和“取消收藏”不同的单元格。
注意:我尝试直接在类中添加手势识别器,但由于某种原因,图像发生了变化,但它突出显示了多个单元格,而不是被点击的特定单元格
我的代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleExampleSubCell
cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
cell.infoLine2TextVw.text = ""
cell.infoLine3TextVw.text = ""
if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
cell.headerTextVw.text = heading_name
cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
}
cell.bringSubview(toFront: cell.headerTextVw)
cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
return cell
}
@objc func AddFavorite(withSender sender:UIButton){
print("clicked")
//The line below fails each time I run it.
sender.setImage(newImage.png,.normal)
}
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewcell uitapgesturerecognizer