【发布时间】:2020-04-18 20:37:41
【问题描述】:
我有一个 UICollectionViewController 用作我的主要 VC,然后一个 UICollectionViewCell 设置为顶部作为标题。但是当我尝试按下单元格内的按钮时,我无法得到回应。我正在为我的 viewDidLoad 使用 initWithFrame。此单元格上的所有按钮都是这种情况。有什么想法吗?
来自 UICollectionViewCell 或标题的代码
let editProfileFollowButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Loading", for: .normal)
button.layer.cornerRadius = 3
button.layer.borderColor = UIColor.lightGray.cgColor
button.layer.borderWidth = 0.5
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.setTitleColor(.black, for: .normal)
button.isEnabled = true
button.addTarget(self, action: #selector(handleButtonFunction), for: .touchUpInside)
return button
}()
@objc func handleButtonFunction() {
print("button clicked")
}
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(editProfileFollowButton)
editProfileFollowButton.anchor(top: postLabel.bottomAnchor, left: postLabel.leftAnchor, bottom: nil, right: self.rightAnchor, paddingTop: 12, paddingLeft: 12, paddingBottom: 0, paddingRight: 20, width: 0, height: 30)
}
【问题讨论】:
-
您是否禁用了单元格选择 (
allowsSelection = no)? -
@MAT - 不,我没有向单元格或 CollectionView 添加任何禁用行。
-
您是否获得了按钮点击效果(按下)?
-
@Kamran 是的,它像我点击它一样闪烁,但我没有从目标获得打印语句。
-
@ShawnaBoucher 看起来很奇怪。至少它应该打印
button clicked语句。我建议在 github 上的某处分享完整的文件或示例示例。
标签: swift uibutton uicollectionviewcell ios13