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