我创建了一个按钮,我已将其分配给我的accessoryView。我看到了创建的按钮,但当我按下它时,没有任何反应。谁能看到我做错了什么?
let accessoryButton = UIButton(type: .roundedRect)
accessoryButton.setTitle("message", for: .normal)
accessoryButton.sizeToFit()
accessoryButton.addTarget(self, action: #selector(goToMessaging(sender:)), for: UIControlEvents.touchUpInside)
cell.accessoryView = accessoryButton as UIView
@objc func goToMessaging(sender: UIButton){
print("message him")
}
试试这个
@objc func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = ...
cell.accessoryType = UITableViewCellAccessoryType.detailButton
cell.tintColor = .red
}
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
print("accessory Button Tapped = \(indexPath.row)")
}
可能是因为你将按钮投射到UIView?
accessoryButton as UIView
如果必须是UIView,您可以尝试添加tapView识别器到accessoryView。