当我单击表格 View 单元格时,单击事件不起作用。我分配了要在自定义 tableView 单元格类中查看的数据,并从 viewController 类传递了值。将数据分配给自定义 TableView 单元格类中的 View 是否有任何问题
ViewController.class
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.ViewAllTableView.dequeueReusableCell(withIdentifier: "ViewAllTableViewCell", for: indexPath) as! ViewAllTableViewCell
let products = self.allProducts[indexPath.row]
cell.setData(products: products)
cell.delegate = self
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "allDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ProductDetailViewController{
let products = self.allProducts[(ViewAllTableView.indexPathForSelectedRow?.row)!]
destination.productID = products.id
}
}
这是自定义表格 View 单元格类
ViewAllTableViewCell.class
class ViewAllTableViewCell: UITableViewCell {
@IBOutlet weak var ItemImage: UIImageView!
@IBOutlet weak var ItemName: UILabel!
@IBOutlet weak var ItemOfferPrice: UILabel!
@IBOutlet weak var ItemOriginalPrice: UILabel!
@IBOutlet weak var ItemWeight: UILabel!
@IBOutlet weak var ItemCountLabel: UILabel!
var delegate : ViewAllTableViewCellDelegate?
var allProduct: ViewAllProductsData!
func setData(products: ViewAllProductsData){
self.allProduct = products
self.ItemName.text = allProduct.name
self.ItemWeight.text = "\(allProduct.quantity) \(allProduct.unit)"
self.ItemOfferPrice.text = "\(allProduct.price)"
self.ItemOriginalPrice.text = "\(allProduct.originalPrice)"
self.ItemCountLabel.text = "\(allProduct.count)"
let url: URL = NSURL(string: allProduct.image)! as URL
self.ItemImage.af_setImage(withURL: url)
}
@IBAction func ViewAllMinusButton(_ sender: UIButton) {
delegate?.minusCount(data: allProduct)
}
@IBAction func ViewAllPlusbutton(_ sender: UIButton) {
delegate?.addCount(data: allProduct)
}
}
protocol ViewAllTableViewCellDelegate{
func addCount(data: ViewAllProductsData)
func minusCount(data: ViewAllProductsData)
}
最佳答案
发生这种情况是因为您没有正确遵守 UITableViewDelegate
或您的 View Controller 中某处有 UITapGestureRecognizer
。
关于iOS TableView 点击事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250103/