如何知道哪个picview在哪个tableview单元格中被触摸?

时间:2016-10-05 13:51:24

标签: ios swift uitableview uitapgesturerecognizer

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

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 个答案:

答案 0 :(得分:2)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

在视图控制器中符合它:

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