我创建了一个包含UIView的集合视图单元,并且在UIView内有一个按钮。 我想做的是单击按钮时将更改UIView边框颜色。 然后,我从服务器加载数据并将其显示在集合视图中。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CypherCollectionViewCell", for: indexPath as IndexPath) as! CypherCollectionViewCell
cell.tickButton.addTarget(self, action: #selector(tickButtonClicked(sender:)), for: .touchUpInside)
return cell
}
@objc func tickButtonClicked( sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
// To change the UIView border color
} else {
sender.isSelected = true
// To change the UIView border color
}
}
谢谢!
答案 0 :(得分:1)
您可以将按钮点转换为视点并获得所需的单元格。
static func changeButtonStyle(fullString: NSString, boldPartsOfString: String, changeFontString: String) -> NSAttributedString {
let nonBoldFontAttribute = [
NSFontAttributeName: MyFont.caption2,
NSForegroundColorAttributeName: MyColor.white
]
/* convert NSString to be added as NSMutableAttributedString */
let changeThemeString = NSMutableAttributedString(string: fullString as String, attributes: nonBoldFontAttribute)
if boldPartsOfString.count > 0 {
let boldFontAttribute = [NSFontAttributeName: MyFont.subheading]
changeThemeString.addAttributes(boldFontAttribute, range: fullString.range(of: boldPartsOfString))
}
if changeFontString.count > 0 {
let colorFontAttribute = [NSForegroundColorAttributeName: MyColor.darkPink]
changeThemeString.addAttributes(colorFontAttribute, range: fullString.range(of: changeFontString))
}
return changeThemeString
}