[英]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.