So i'm trying to create a popover that appears when clicking a view inside a tableview cell. Here's what I've tried so far. This is what I have inside my 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)
}
}
This is what i have inside my view controller:
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
}
identifier is correct. The anchor is to the tableview for the segue. I've never actually created a tappable view before, but did do the same thing with a button just and it worked fine. No errors, app just crashes when clicking the view.
I'm guessing it could be something to do with me not adding it into cellForRowAtIndexPath for the individual cell. How would this be done with a view if i cant addTarget?
You are calling openingHoursTap:
in your table view cell but your actual implementation is in your view controller. That's why it crashes. So the correct way to do this is add the tap gesture recogniser in tableView: cellForRowAtIndexpath:
in your view controller.
Example:
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
let tap = UITapGestureRecognizer(target: self, action:Selector("openingHoursTap:"))
cell.openingHoursView.addGestureRecognizer(tap)
}