简体   繁体   English

当UITapGestureRecognizer在prepareForSegue中点击并捕获其中的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

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 首先,我有一个自定义表格单元格,其描述如下

在此处输入图片说明

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. 现在,当用户点击单元格本身时,它将转到视图A,但是,有一个UITapGestureRecognizer连接到左侧的UIImage,该UIImage连接到转到视图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. 一切都很好,但是我需要一些可以在表视图单元格中传递给视图B的数据,以便在显示视图后可以做一些事情。

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

Perhaps I should just set something as a tag of UIImage or the gesture object when initializing? 也许我应该在初始化时设置一些东西作为UIImage的标签或手势对象? Or should I avoid trying to do this in prepareForSegue in the first place? 还是我应该首先避免在prepareForSegue中尝试这样做?

Still new to this so any advice is greatly appreciated. 这仍然是新事物,因此非常感谢您提出任何建议。

I would suggest that putting the segue inside the table view cell is the wrong place. 我建议将segue放在表格视图单元格内是错误的地方。 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 - 然后声明一个delegate属性,并在手势识别器中使用它-

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PrepareForSegue方法中获取自定义单元格的indexPath-iOS - How to get indexPath of a custom cell in PrepareForSegue method - iOS 轻按单元格中的UIImageView时如何获取indexpath.row? - How to get the indexpath.row when a UIImageView in a cell is tapped? 如何在UIMenuController中的自定义操作中获取轻敲的表格视图单元格 - How to get the tapped table view cell in custom action in UIMenuController 如何在 prepareForSegue 中获取索引路径 - How to get indexpath in prepareForSegue 当点击collectionView单元格内的按钮时,如何获取表格视图行并从位于tableViewCell内的collectionView推送到viewController - how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell 如何将 UITapGestureRecognizer 添加到表视图单元格内的 UILabel? - How can I add a UITapGestureRecognizer to a UILabel inside a table view cell? 从prepareForSegue访问表视图IndexPath - Accessing a Table View IndexPath from prepareForSegue 在prepareForSegue函数中搜索数据时如何获取indexPath - How to get indexPath when I search data in the function that prepareForSegue 使用UITapGestureRecognizer从窃听的单元格获取标签文本 - Get label text from tapped cell with UITapGestureRecognizer 如何从表格视图单元格中的图像手势获取 indexPath - How to get indexPath from image gesture in table view's cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM