创建一个 popover segue 点击一个 tableview 单元格内的视图

create a popover segue clicking a view inside a tableview cell

所以我正在尝试创建一个弹出窗口,当单击表格视图单元格内的视图时会出现该弹出窗口。到目前为止,这是我尝试过的。 这是我的 customcell 中的内容:

class Cell: UITableViewCell {

@IBOutlet weak var openingHoursView: CustomView!

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

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}
}

这是我的视图控制器中的内容:

    else if segue.identifier == "openingHours" {
        var vc = segue.destinationViewController
        var controller = vc.popoverPresentationController
        if controller != nil {
            controller?.delegate = self
        }
    }

@IBAction func openingHoursTap(sender: UITapGestureRecognizer) {
    performSegueWithIdentifier("openingHours", sender: self)
}


func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
}

标识符正确。锚点是 segue 的 tableview。我以前从未真正创建过可点击视图,但只是用一个按钮做了同样的事情,而且效果很好。没有错误,应用程序只是在单击视图时崩溃。

我猜这可能与我没有将它添加到单个单元格的 cellForRowAtIndexPath 中有关。如果我不能 addTarget,如何使用视图完成此操作?

您在 table 视图单元格中调用 openingHoursTap:,但您的实际实现是在视图控制器中。这就是它崩溃的原因。因此,正确的做法是在视图控制器的 tableView: cellForRowAtIndexpath: 中添加点击手势识别器。

示例:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {<br> 让单元格 = tableView.dequeueReusableCellWithIdentifier("Cell")<br> 让 tap = UITapGestureRecognizer(target: self, action:Selector("openingHoursTap:"))<br> cell.openingHoursView.addGestureRecognizer(点击)<br> }