是因为UITableViewCell默认情况下不具备响应点击事件的能力。要使UITableViewCell具备点击响应功能,可以通过以下几种方式实现:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// 执行点击操作
// ...
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellTapped(_:)))
cell.addGestureRecognizer(tapGesture)
@objc func cellTapped(_ gesture: UITapGestureRecognizer) {
// 执行点击操作
// ...
}
class CustomCell: UITableViewCell {
var button: UIButton!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
button = UIButton(type: .system)
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
// 设置按钮的样式、位置等
// ...
contentView.addSubview(button)
}
@objc func buttonTapped(_ sender: UIButton) {
// 执行按钮点击操作
// ...
}
}
以上是在没有UIButton的情况下使UITableViewCell具备点击响应功能的几种方法。根据具体需求和场景选择合适的方式来实现。对于腾讯云相关产品和产品介绍链接地址,可以参考腾讯云官方文档或官方网站获取更详细的信息。
领取专属 10元无门槛券
手把手带您无忧上云