如何使用UITableView点击图片和标题



我创建了一个tableView,但点击它时,我没有得到任何结果。我想添加一个新功能来改进我的项目,但是我不能添加我看过的视频和我研究过的东西。

表视图

import UIKit
class ViewController1: UIViewController {
@IBOutlet weak var FoodView: UITableView!

let dogfoods = ["pork", "banana", "chicken-leg"]

override func viewDidLoad() {
super.viewDidLoad()

FoodView.delegate = self
FoodView.dataSource = self
// not tapped no see
FoodView.allowsSelection = false
}


}
extension ViewController1: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}





func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dogfoods.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = FoodView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
let dogfood = dogfoods[indexPath.row]
cell.foodImageView.image = UIImage(named: dogfood)
cell.nameLabel.text = dogfood

return cell
}


}

CustomCell

class CustomCell: UITableViewCell {
@IBOutlet weak var dogView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var foodImageView: UIImageView!



override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}

当我点击图片中的一个单元格时,我想要写一个更大的图片和描述,我该怎么做?当我在网上搜索时,我用了类似的方法,但是我找不到任何结果。

您有几个选择。您可以向自定义单元格添加一个或多个按钮。您可以在单元格的内容视图中附加一个轻击手势识别器。

响应整个cell上的点击最简单的方法可能是让视图控制器符合UITableViewDelegate协议并实现tableView(_:didSelectRowAt:)方法。

该方法将在用户选择一个单元格时被调用,您将使用被选中单元格的indexPath来确定哪个单元格被选中,并执行适当的操作。

您可以使用Swift中的Delegate轻松有效地做到这一点

//使用Java接口创建协议

@objc protocol TableViewCellDelegate {
@objc func click(indexPath: IndexPath?)
}

//修改你的类CustomCell为:

@IBOutlet weak var dogView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var foodImageView: UIImageView!
var delegate: TableViewCellDelegate?
var indexPath: IndexPath?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
foodImageView.isUserInteractionEnabled = true
foodImageView.addGestureRecognizer(tapGestureRecognizer)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
let tappedImage = tapGestureRecognizer.view as! UIImageView
self.delegate.click?(indexPath: self.indexPath)
}

//修改ViewController1中的tableView(_ tableView: UITableView, cellForRowAt indexPath: indexPath)

let cell = FoodView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
cell.delegate = self
cell.indexPath = indexPath
let dogfood = dogfoods[indexPath.row]
cell.foodImageView.image = UIImage(named: dogfood)
cell.nameLabel.text = dogfood
return cell

//现在添加扩展添加ViewController1的结束

extension ViewController1: TableViewCellDelegate {
func click(indexPath: IndexPath?) {
// open image for preview here
}
}

你可以通过多种方式做到这一点

  1. 使用add a UITableViewDelegate方法

    覆盖函数tableView(_ tableView: UITableView, didSelectRowAt)indexPath: indexPath) {//你的代码…


    }

每次点击cell时它都会被调用。

  1. 如果你更喜欢触发任何按钮点击,而不是单元格点击,然后去委托(Izaan Saleem已经解释过),或者你可以使用NotificationCenter,但对于这个任务,我更喜欢didSelect或委托解决方案。

最新更新