How to know which pic in which tableview cell has been touched?

浪子不回头ぞ 提交于 2019-12-12 00:37:33

问题


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
}

}

回答1:


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

    }

}


来源:https://stackoverflow.com/questions/39875851/how-to-know-which-pic-in-which-tableview-cell-has-been-touched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!