0

I'm facing a problem in using UITapGestureRecogniser for an image in tableViewCell. I've created an outlet of object UIImageView in tableViewCell and my requirement is whenever I click on that imageView other image has to be displayed. I've tried the code below, but it didn't work.

Any help would be appreciated!!

Cell

import UIKit

class resultsTableViewCell: UITableViewCell {
  @IBOutlet weak var star_selected: UIImageView!
  @IBOutlet weak var star_unselected: UIImageView!

  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
  }

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
  }
}

View controller

class resultsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {
  @IBOutlet weak var resultsTable: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()
    flightCodeView = FlightCodeView(frame: CGRect.zero)
    self.view.addSubview(flightCodeView)
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultsCellIdentifier", for: indexPath) as! resultsTableViewCell

    let image : UIImage = UIImage(named: "star_unselected")!
    cell.star_unselected.image = image

    let tapGesture = UITapGestureRecognizer(target: self, action: Selector(("ImageSelected")))
    cell.star_unselected.addGestureRecognizer(tapGesture)
    tapGesture.delegate = self
    return cell
  }

  func ImageSelected(sender: UITapGestureRecognizer? = nil) {
    // just creating an alert to prove our tap worked!
    let image: UIImage = UIImage(named: "star_selected.png")!
    cell.star_selected.image = image
  }
}
User511
  • 1,456
  • 10
  • 28
sindhu kopparati
  • 223
  • 1
  • 3
  • 7
  • call this function in outside the cell class, func ImageSelected(sender: UITapGestureRecognizer? = nil) { print("yess") // just creating an alert to prove our tap worked! let image : UIImage = UIImage(named: "star_selected.png")! cell.star_selected.image = image } – Anbu.Karthik Feb 08 '17 at 07:39
  • Check this: http://stackoverflow.com/questions/39875851/how-to-know-which-pic-in-which-tableview-cell-has-been-touched/39876440#39876440 – Bista Feb 08 '17 at 07:54
  • by default, the UIImageView will**not** be enabled for userInteraction, so make sure to set that, either in StoryBoard or code – Russell Feb 08 '17 at 08:14
  • Add `isUserInteractionEnabled = true` inside your method awakeFromNib from your cell. Add your gesture recognized there also. – Leo Dabus Feb 08 '17 at 09:56
  • BTW it is Swift convention to name your classes starting with a lowercase letter – Leo Dabus Feb 08 '17 at 09:58

3 Answers3

0

Check this: imageView.isUserInteractionEnabled = true

jia ma
  • 198
  • 5
0

add this code just below tapgesure variable ...

cell.userInteractionEnabled = true
cell.addGestureRecognizer(tapGesture)

I hope this will help..

Naveed Khan
  • 338
  • 4
  • 16
0

Add this code in cellForRowAt:

cell.star_unselected.userInteractionEnabled = true

The function will look like :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "resultsCellIdentifier", for: indexPath) as! resultsTableViewCell

let image : UIImage = UIImage(named: "star_unselected")!
cell.star_unselected.image = image

cell.star_unselected.userInteractionEnabled = true

let tapGesture = UITapGestureRecognizer(target: self, action: Selector(("ImageSelected")))
cell.star_unselected.addGestureRecognizer(tapGesture)
tapGesture.delegate = self
return cell

}

Sneha
  • 2,200
  • 6
  • 22
  • 36