【发布时间】:2018-08-16 04:49:55
【问题描述】:
我创建了一个包含 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
}
}
谢谢!
【问题讨论】:
-
代码看起来不错,检查是否启用了用户交互。
-
代码看起来不太好。你用什么来改变视图边框的颜色?
cell.view.borderColor?如果是这样,您从ticketButtonClicked中的哪里获得单元格? -
这是一个糟糕的实现。您应该在单元格内有按钮操作。
-
对不起,错过了。