If you add UITapGestureRecognizer
to an UIImageView
then don't
forget to set isUserInteractionEnabled
property to true
.
Create a variable to store selected indexPath.row
var selectedCell:Int!
In cellForRow
method add gesture to the image view.
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBtnAction(_:)))
cell.imageView.tag = indexPath.row
cell.imageView.addGestureRecognizer(tapGesture)
In target method perform segue
func tapBtnAction(_ sender: UITapGestureRecognizer) {
print("(sender.view.tag) Tapped")
self.selectedCell = sender.view.tag
self.performSegueWithIdentifier("detailSegue", sender: self)
}
In prepareforsegue
method get data from the array.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue" {
let next: DetailViewController = segue.destination as! DetailViewController
next.data = datas[self.selectedCell]
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…