0

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
}

}
beginner_T
  • 417
  • 1
  • 6
  • 21
  • You already have tap gestures on each image view, so now you know which image is touched. What is the problem? – Hossam Ghareeb Oct 05 '16 at 13:56
  • true, but the tap gesture is inside the tableViewCell class. So how do I get the indexPath from there? I just know that I can get the indexPath from the UITableView inside the UITableView class, but then I would need to put another gesture recognizer on the images which seems to be wrong?! – beginner_T Oct 05 '16 at 14:00
  • 1
    You need a delegate to fire an event in the view controller that the cell has been touched. ViewController has reference to the table view and can give you the indexPath by calling tableview.indexPathForCell function – Hossam Ghareeb Oct 05 '16 at 14:03
  • can you maybe show me how to do it? I don't really get what you mean. – beginner_T Oct 05 '16 at 14:13
  • This answer may help http://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11 Oct 05 '16 at 14:16
  • @Mr.UB has answered you with the same idea. I was going to post the code but he was faster :) – Hossam Ghareeb Oct 05 '16 at 14:21
  • thanks anyways for your help! – beginner_T Oct 05 '16 at 14:23

1 Answers1

2
protocol TwoPicsTableViewCellDelegate{
    func image1Clicked(cell:TwoPicsTableViewCell)
    func image2Clicked(cell:TwoPicsTableViewCell)
}

class TwoPicsTableViewCell: UITableViewCell {

    var delegate:TwoPicsTableViewCellDelegate!

    func checkPicTwo() {

        firstImage.bouncing()
        vote(voteForPic: firstImage)

        if secondImage.layer.borderWidth != 0 {
            secondImage.layer.borderWidth = 0
        }

        //delegate call
        delegate.image1Clicked(self)
    }

    func checkPicOne() {

        secondImage.bouncing()

        if firstImage.layer.borderWidth != 0 {
            firstImage.layer.borderWidth = 0
        }

        //delegate call
        delegate.image2Clicked(self)
    }
}

In the view controller conform it:

class myViewController: UIViewController, TwoPicsTableViewCellDelegate{


    //implement delegate methods

    func image1Clicked(cell:TwoPicsTableViewCell){
        let indexPath = tableview.indexPathForCell(cell)
        print(indexPath)
    }

    func image2Clicked(cell:TwoPicsTableViewCell){
        let indexPath = tableview.indexPathForCell(cell)
        print(indexPath)
    }


    func table cellForRowAt..........{
        let cell = table.dequeCellAt..........   as? TwoPicsTableViewCell
        //Assign delegate
        cell.delegate = self
        .......
        .......

        return cell

    }

}
Bista
  • 7,869
  • 3
  • 27
  • 55
  • Thanks a lot! I will try it as soon as possible! – beginner_T Oct 05 '16 at 14:18
  • have defined it in table cell class as i have mentioned in above answer? `var delegate:TwoPicsTableViewCellDelegate!` – Bista Oct 05 '16 at 14:26
  • that was my mistake :D building the app works, but when I tap an image I get an error: unexpectedly found nil while unwrapping an optional value :( – beginner_T Oct 05 '16 at 20:48
  • sorry, my mistake again :D I put the cell.delegate = self into the wrong cell.. works perfectly!!! Thanks a lot! – beginner_T Oct 05 '16 at 20:50
  • For some reason it works for all the TwoPicsTableViewCells, except of the very first one?! You have any idea why? I can't click the first cell at all anymore – beginner_T Oct 05 '16 at 20:56
  • I now noticed it's not just the first one.. why do some work perfectly and some just don't react at all? I can't even click them – beginner_T Oct 05 '16 at 21:13
  • It should not happen, are the images too small? – Bista Oct 06 '16 at 03:54
  • 1
    I fixed it.. The problem was, that first, I just have like a default cell of type twoPicsTableViewCell which just says "loading" and the imageViews are hidden... then I load the data from firebase and reload the tableView with the data and show the imageViews again... I still don't know why that effects some random cells, but when I don't put the default cell before loading the cells (so I just leave the tableView empty and then load the cells), then it works :) – beginner_T Oct 06 '16 at 12:57