当我单击表格视图单元格单击事件不起作用时。我被分配了数据以在自定义tableView单元类中查看并从viewController类传递值。在自定义表视图单元类
中为视图分配数据时是否有任何问题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
}
}
这是自定义表格视图单元格类
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)
}
答案 0 :(得分:1)
将tableView属性选择从无选择更改为单选
后,它对我有用答案 1 :(得分:0)
之所以发生这种情况,是因为您未能正确地与UITableViewDelegate
保持一致,或者您的视图控制器中某处UITapGestureRecognizer
。