如何知道哪个tableview单元格中的哪张图片被触摸了? [英] How to know which pic in which tableview cell has been touched?

查看:25
本文介绍了如何知道哪个tableview单元格中的哪张图片被触摸了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 tableView 中有自定义的 tableViewCells.它们在单元格内有 2 个或更多 ImageViews,可以触摸它们,然后它们会反弹并被选中.但是既然单元格被重用了,我怎么知道哪一行中的哪个 ImageView 被选中了?我知道我可以获得 indexPath.row,但是我怎么知道 2 或 3 个图像中的哪一个触发了该函数来为我提供 indexPath?希望你们明白我的意思.这是我的自定义 tableViewCell,tableView 代码只是有点标准.

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
}

}

推荐答案

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

    }

}

这篇关于如何知道哪个tableview单元格中的哪张图片被触摸了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆