我的 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
}
}
最佳答案
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)
}
}
在 View Controller 中符合它:
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
}
}
关于ios - 如何知道触摸了哪个tableview单元格中的哪张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875851/