iOS TableView 点击事件不起作用

标签 ios swift uitableview

当我单击表格 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/

相关文章:

ios - 将图像添加到导航栏

iOS Storyboards/NIBs 底层剖析。它们是如何实现的?

ios - swift glGetProgramInfoLog

ios - 无法找到崩溃原因

ios - iOS 服务广告在后台如何运作?

ios - 如何使用 sectionNameKeyPath (Swift) 对 tableView 数据进行分组

Swift Remove 1 节点在碰撞后移动

ios - UITableViewCell 复选标记切换不起作用

uitableview - ios7.1模拟器中Swift重用cells,cells被隐藏

iphone - UITableView 标题 View 滚动