如何在prepareForSegue中由UITapGestureRecognizer轻击并捕获其中的UIImage时获取自定义表视图单元的索引路径 [英] How to get the indexpath of a custom table view cell when an UIImage inside of it is tapped and captured by UITapGestureRecognizer in prepareForSegue

查看:103
本文介绍了如何在prepareForSegue中由UITapGestureRecognizer轻击并捕获其中的UIImage时获取自定义表视图单元的索引路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题中有很多变量,对不起,我无法让它变得更简单。

Ok a lot of variables in the title, sorry I couldn't make it any more simpler.

首先,我有一个自定义表格单元格,其中包含类似描述

First, I have a custom table cell with descriptions like so

现在,当用户点击单元格时,它会转到View A,
但是,有一个连接的UITapGestureRecognizer到左边的UIImage,它连接到一个转到View B的segue。

now, when a user taps on the cell itself, it would go to View A, however, there is a UITapGestureRecognizer that is connected to the UIImage at the left, which is connected to a segue that goes to View B.

一切都很好,但我需要一些在表格视图中的数据我可以传递给View B,这样一旦显示视图就可以做一些事情。

All is fine, but I need some data that is inside the table view cell that I can pass to View B so it can do some stuff once the view is shown.

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "toViewA") {

    }else if( segue.identifier == "toViewBFromThatImage" ){

        let viewBVC = ( segue.destinationViewController as! viewBVC )
        ///////sender is UITapGestureRecognizer 
        ///////somehow want to get indexpath here

         viewBVC.selectedIndex = SOMEHOW DIRREVED INDEXPATH
    }
}

也许我应该在初始化时将某些东西设置为UIImage的标签或手势对象?或者我应该首先避免在prepareForSegue中尝试这样做?

Perhaps I should just set something as a tag of UIImage or the gesture object when initializing? Or should I avoid trying to do this in prepareForSegue in the first place?

这还是新的,所以任何建议都非常感谢。

Still new to this so any advice is greatly appreciated.

推荐答案

我建议将segue放在表格视图单元格中是错误的地方。它属于视图控制器,在视图控制器上调用委托方法来执行它。

I would suggest that putting the segue inside the table view cell is the wrong place. It belongs in the view controller, with a delegate method invoked on the view controller to perform it.

在您的单元子类中声明一个协议 -

Declare a protocol in your cell subclass -

protocol MyCustomCellDelegate {
    func imageTappedInCell(cell:MyCustomCell)
}

然后声明一个委托属性并在你的手势识别器中使用它 -

Then declare a delegate property and use it in your gesture recogniser -

class MyCustomCell {

    weak var delegate : MyCustomCellDelegate?

    ...

   func imageTapped(recognizer:UIGestureRecognizer) {

       if (recognizer.state == .Ended) {
           delegate?.imageTapped(self)
       }

   }

然后在视图控制器中,您可以实现委托方法。在委托方法中,您可以使用单元格来标识索引路径

Then in your view controller you can implement the delegate method. In the delegate method you can use the cell to identify the index path

 class MyTableViewController: UIViewController,MyCustomCellDelegate {


     func imageTapped(cell:MyCustomCell) {
         self.performSegueWithIdentifier("toViewBFromThatImage",sender:cell)
     }

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if( segue.identifier == "toViewBFromThatImage" ){
            let viewBVC = ( segue.destinationViewController as! viewBVC )
            let senderCell=sender as! MyCustomCell
            viewBVC.selectedIndex = self.tableview.indexPathForCell(senderCell)!
        }
    }
}

这篇关于如何在prepareForSegue中由UITapGestureRecognizer轻击并捕获其中的UIImage时获取自定义表视图单元的索引路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆