【发布时间】:2021-01-28 15:04:06
【问题描述】:
在我的集合视图中,有 6 个单元格。每个单元格都有一个按钮。如果我将点击任何按钮,则按钮的图像将更改为标记图像,所有其他单元格按钮将更改为取消标记图像。请任何人帮助我。我在这里添加了代码和图片。
在这张图片中,有一个表格视图,在表格视图单元格内有一个集合视图。
我的代码是:-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InsuranceCollectionViewCell", for: indexPath) as? InsuranceCollectionViewCell else {
return UICollectionViewCell()
}
cell.selectionButton.tag = indexPath.row
let insuranceTypeAtIndex = insuranceTypeArray[indexPath.row]
cell.insuranceTypeLabel.text = insuranceTypeAtIndex
cell.selectionButton.addTarget(self, action: #selector(selectButtonAction(sender:)), for: .touchUpInside)
return cell
}
@objc func selectButtonAction(sender: UIButton){
let index = IndexPath(row: sender.tag, section: 0)
let cell = insuranceTypeCollectionView.cellForItem(at: index) as? InsuranceCollectionViewCell
if cell?.selectionButton.currentImage == UIImage(named: "untick"){
cell?.selectionButton.setImage(UIImage(named: "tick"), for: .normal)
print(cell?.insuranceTypeLabel.text)
}
else{
cell?.selectionButton.setImage(UIImage(named: "untick"), for: .normal)
}
}
【问题讨论】: