ios – Swift:如何在UITableViewCell中创建可点击的UIView?

在UITableViewCell内部,我试图实现一个包含图像和文本的按钮.

似乎标准的UIButton无法实现这一点.所以我创建了一个包含UIImageView和UILabel的UIView.

您可以在右侧看到实现,“跟随行程”按钮(“”是UIImageView,“跟随行程”是UILabel)

我现在正试图使这样的UIView(即按钮)可点击,但我找不到办法.

这是我的实现,但它不起作用:

class StationsIntroHeader: UITableViewCell {

    @IBOutlet weak var bigButton: UIView!

    override  func awakeFromNib() {
        super.awakeFromNib()
        let tap = UITapGestureRecognizer(target: self,action: Selector("followTrip:"))
        bigButton.addGestureRecognizer(tap)
    }

    func followTrip(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}

我确保在UIVmageView和UILabel上的UIView和OFF上启用了User Interaction Enabled

解决方法

对我来说,如下所示的示例设置完全有效:
class TableViewController: UITableViewController {
  override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return 5
  }

  override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return tableView.dequeueReusableCellWithIdentifier("CustomCell",forIndexPath: indexPath)
  }
}

class CustomCell: UITableViewCell {
  @IBOutlet weak var bigButton: UIView!

  override func awakeFromNib() {
    super.awakeFromNib()
    let tap = UITapGestureRecognizer(target: self,action: Selector("bigButtonTapped:"))
    bigButton.addGestureRecognizer(tap)
  }

  func bigButtonTapped(sender: UITapGestureRecognizer) {
    print("bigButtonTapped")
  }
}

我没有为视图或imageview或标签更改userInteractionEnabled的任何认值.将我的实施与你的实施进行比较,看看你是否忘记了某些事情…连接插座?

示例项目:https://www.dropbox.com/sh/hpetivhc3gfrapf/AAAf6aJ0zhvRINPFJHD-iMvya?dl=0

编辑您的项目

func tableView(tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
  let headerCell = tableView.dequeueReusableCellWithIdentifier("StationsIntroHeader") as! StationsIntroHeader
  headerCell.update()
  return headerCell
  // return headerCell.update().contentView
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...