Swift: 如何在 UITableViewCell 中使 UIView 可点击?

Swift: how to make a UIView clickable in UITableViewCell?

UITableViewCell 中,我正在尝试使用 image 实现 button ]文本.

似乎标准 UIButton 无法做到这一点。所以我创建了一个 UIView 其中包含一个 UIImageView 和一个 UILabel.

您可以在右侧看到实现,"follow trip" 按钮(“+”是一个 UIImageView,"follow trip" 是一个 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")
    }
}

我已确保 启用用户交互UIView 上处于开启状态,在 UIImageViewUILabel[= 上处于关闭状态22=]

您可以简单地将 UIButton 子类化,并在其中实现自定义绘图。 UIButton 是在不干扰 table 视图触摸的情况下完成任务的最简单方法。如果您在 table 视图上实现点击手势,将会导致单元格触摸出现问题。

您可以简单地获取自定义图像(+ 符号和文本作为一个图像)并将其用作背景图像。

或者你甚至可以用代码画出来

例如,你可以试试这个:

 func drawCanvas1(frame frame: CGRect = CGRectMake(3, 8, 209, 109)) {
    //// General Declarations
    let context = UIGraphicsGetCurrentContext()

    //// Color Declarations
    let color = UIColor(red: 0.967, green: 0.423, blue: 0.211, alpha: 1.000)

    //// Image Declarations
    let screenShot20151111At32900PM = UIImage(named: "screenShot20151111At32900PM.png")!

    //// Rectangle Drawing
    let rectanglePath = UIBezierPath(roundedRect: CGRectMake(frame.minX + 39, frame.minY + 23, 113, 46), cornerRadius: 8)
    color.setFill()
    rectanglePath.fill()


    //// Rectangle 2 Drawing
    let rectangle2Rect = CGRectMake(frame.minX + 51, frame.minY + 27, 33, 34)
    let rectangle2Path = UIBezierPath(rect: rectangle2Rect)
    CGContextSaveGState(context)
    rectangle2Path.addClip()
    screenShot20151111At32900PM.drawInRect(CGRectMake(floor(rectangle2Rect.minX - 16 + 0.5), floor(rectangle2Rect.minY - 15 + 0.5), screenShot20151111At32900PM.size.width, screenShot20151111At32900PM.size.height))
    CGContextRestoreGState(context)


    //// Text Drawing
    let textRect = CGRectMake(frame.minX + 97, frame.minY + 23, 73, 46)
    let textTextContent = NSString(string: "\nfollow\ntrip\n")
    let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
    textStyle.alignment = .Left

    let textFontAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.labelFontSize()), NSForegroundColorAttributeName: UIColor.whiteColor(), NSParagraphStyleAttributeName: textStyle]

    let textTextHeight: CGFloat = textTextContent.boundingRectWithSize(CGSizeMake(textRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size.height
    CGContextSaveGState(context)
    CGContextClipToRect(context, textRect);
    textTextContent.drawInRect(CGRectMake(textRect.minX, textRect.minY + (textRect.height - textTextHeight) / 2, textRect.width, textTextHeight), withAttributes: textFontAttributes)
    CGContextRestoreGState(context)
}

结果将类似于:

对我来说,像下面这样的示例设置完全有效:

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")
  }
}

我没有更改视图或图像视图或标签的任何默认值 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
}