从UITableViewCell中点击单元格内的图像

一直试图弄清楚这一点,现在和几个小时寻找解决scheme后,我决定是时候问。

我有一个tableview,由自定义的UITableViewCells填充,当前你点击一个单元格,它会带你到一个细节视图。

在自定义的单元格中有一个图像,我希望用户能够点击该图像,并继续显示图像的popup窗口VC。

我遇到的麻烦是当图像被挖掘时创buildsegue。

在自定义单元格的文件中,我在图像上设置了一个轻手势识别器(pTap)

override func awakeFromNib() { super.awakeFromNib() let tap = UITapGestureRecognizer(target: self, action: #selector(PostCell.voteTapped(_:))) let ptap = UITapGestureRecognizer(target: self, action: #selector(PostCell.imageTapped(_:))) tap.numberOfTapsRequired = 1 ptap.numberOfTapsRequired = 1 voteImage.addGestureRecognizer(tap) voteImage.userInteractionEnabled = true featuredImg.addGestureRecognizer(ptap) featuredImg.userInteractionEnabled = true } 

我在自定义单元格文件中还有一个function:

 func imageTapped(sender: UIGestureRecognizer) { print("image tapped") } 

在我的视图控制器文件中,我已经在索引path的select行中添加了一个segue:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let post: Post! if inSearchMode { post = filteredVenues[indexPath.row] } else { post = posts[indexPath.row] } print(post?.venueName) performSegueWithIdentifier("imageTapped", sender: nil) performSegueWithIdentifier("DetailsVC", sender: post) } 

此外,在故事板中,我创build了一个从VC的持续表格视图与自定义单元格到我想要显示的图像点击时显示的VC。

我已经尝试了几种不同的方法来使这个工作,并没有任何运气,上面看到的代码是多次失败后仍然是什么。 我觉得在自定义单元格文件中的tap和在VC文件中的segue的function是解决scheme的一部分,所以这就是为什么我已经把它们留在了。

任何帮助,将不胜感激。 谢谢!

从下面的答案更新代码:

新增协议

 protocol ImageSegueProtocol: class { func imageTapped(row: Int) } class PostCell: UITableViewCell { 

增加了IAB Func

 @IBAction func imageTapped(sender: UIGestureRecognizer) { guard let row = row else { return } delegate?.imageTapped(row) print("image tapped func") } 

在主VC中声明委托

 weak var delegate:postCell? 

指定的Delgate

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //let post = posts[indexPath.row] if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell { var img: UIImage? var vImg: UIImage? postCell?.delegate = self 

增加了扩展function

 extension FeedVC: ImageSegueProtocol { func imageTapped(row: Int) { if inSearchMode == true { let object = filteredVenues[row] performSegueWithIdentifier("imageTapped", sender: object) print("ext func") } else { let object = posts[row] performSegueWithIdentifier("imageTapped", sender: object) print("ext func") } } 

你可以这样做:

在单元格文件中,在顶部声明一个协议,然后在单元类本身中设置一些属性,并响应被点击的图像的委托行为:

 protocol SomeCellProtocol: class { func imageTapped(row: Int) } class SomeCell: UITableViewCell { weak var delegate: SomeCellProtocol? var row: Int? @IBAction func imageTapped() { guard let row = row else { return } delegate?.imageTapped(row) } } 

然后你必须让你的ViewController采用SomeCellProtocol并像这样调用segue:

 extension SomeViewController: SomeCellProtocol { func imageTapped(row: Int) { //get object from array and call segue here } } 

你必须通过调用你的ViewController作为你的单元的委托:

 someCell.delegate = self 

并将行传递给单元格:

 someCell.row = indexPath.row 

在ViewController的cellForRowAtIndexPath方法中。

所以,当button在单元格内被轻敲时(或者你可以用ImageView上的GestureRecognizer来实现),它将强制委托(ViewController)调用它的imageTapped函数,传递一个你可以使用的行参数确定表中的哪个对象(其对应的数据数组)应该通过Segue传递。

正如OhadM所说,创build一个button并将背景图像设置为要显示的图像。 从那里,你甚至不需要一个IBAction。 控制 – 从button拖动到下一个视图控制器,并创build赛格

那么,如果你想在赛前做任何设置,在第一个VC中你会有这样的东西:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "imageButtonPressed" { // Make sure you name the segue to match let controller = segue.destinationViewController as! SecondVC controller.someText = "Hi" controller.someInt = 5 } } 

你需要做的是创build一个button,而不是图像,这个button将保存实际的图像:

在这里输入图像说明

其余的很容易,按住ctrl拖动IBAction到你的自定义单元格文件中。 现在,您需要与视图控制器进行通信,以调用您的segue方法。

您可以使用2种devise模式来实现这一点:

  1. 使用发布通知 :

    在你的视图控制器里添加一个观察者:

     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourViewController.cellSelected(_:)), name: "CellSelectedNotificationName", object: nil) 

    您在View Controller中select的方法cellSelected应该如下所示:

     func cellSelected(notification : NSNotification) { if let passedObject = notification.object as? YourCustomObject { // Do what ever you need with your passed object // When you're done, you can invoke performeSegue that will call prepareForSegue // When invoking performeSegue you can pass your custom object } } 

    在您的button的IBAction方法的CustomCell类中:

     @IBAction func buttonTapped() { // Prepare the object you want to pass... NSNotificationCenter.defaultCenter().postNotificationName("CellSelectedNotificationName", object: YourCustomObjectYouWantToPass) } 

    在一个坚果shell中,一个单元格内的button被轻敲,您在该单元格内创build了一个对象,并使用通知中心将其传递给View Controller。 现在,您可以继续使用您的自定义对象。

注意 :如果你不需要传递一个对象,你可以基本上从你的UIButton(在你的故事板)拖动到另一个视图控制器。

  1. 使用指向View Controller的委托 。

祝你好运。