I have custom tableViewCells in my tableView. They have 2 or more ImageViews inside the cell, which can be touched and then they will bounce and be selected. But since the cells are being reused, how do I know which ImageView in which row has been selected? I know I can get the indexPath.row, but how do I know which of the 2 or 3 Images triggered the function to give me the indexPath? Hope you guys get what I mean. Here is my custom tableViewCell, the tableView code is just kinda standard.
import UIKit
class TwoPicsTableViewCell: UITableViewCell {
@IBOutlet var containerView: UIView!
@IBOutlet var votesButton: UIButton!
@IBOutlet var commentsButton: UIButton!
@IBOutlet var firstImage: bouncingRoundImageView!
@IBOutlet var secondImage: bouncingRoundImageView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var descriptionLabel: UILabel!
@IBOutlet var topUsernameLabel: UILabel!
@IBOutlet var bottomUsernameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
//self.layoutIfNeeded()
containerView.layer.cornerRadius = 10
containerView.clipsToBounds = true
self.backgroundColor = ColorScheme.primaryColor
votesButton.setTitleColor(ColorScheme.fourthColor, for: UIControlState())
commentsButton.setTitleColor(ColorScheme.fourthColor, for: UIControlState())
setupBackgroundGradient()
setupFirstImage()
setupSecondImage()
}
func setupBackgroundGradient() {
let bottomColor = ColorScheme.secondaryColor.cgColor
let topColor = ColorScheme.thirdColor.cgColor
let layer = CAGradientLayer()
layer.frame = containerView.frame
layer.frame.offsetBy(dx: -10,dy:-10)
layer.frame.size.width += 10
layer.frame.size.height += 10
layer.colors = [topColor, bottomColor]
containerView.layer.insertSublayer(layer, at: 0)
}
func setupFirstImage() {
let tappedOne = UITapGestureRecognizer(target: self, action: #selector(checkPicTwo))
firstImage.addGestureRecognizer(tappedOne)
}
func setupSecondImage() {
let tappedTwo = UITapGestureRecognizer(target: self, action: #selector(checkPicOne))
secondImage.addGestureRecognizer(tappedTwo)
}
func checkPicTwo() {
firstImage.bouncing()
vote(voteForPic: firstImage)
if secondImage.layer.borderWidth != 0 {
secondImage.layer.borderWidth = 0
}
}
func checkPicOne() {
secondImage.bouncing()
if firstImage.layer.borderWidth != 0 {
firstImage.layer.borderWidth = 0
}
}
override func prepareForReuse() {
super.prepareForReuse()
firstImage.image = nil
secondImage.image = nil
firstImage.layer.borderWidth = 0
secondImage.layer.borderWidth = 0
}
}