作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 VC 名称 RestaurantViewController
它有 itemName ItemPrice 的自定义单元格,并且有三个按钮 + , - 现在当用户通过加/减设置选择数量然后点击添加按钮整个数据时,添加他们的操作在 customCellClass应该转到 CartVC
但在用户选择主菜单上的购物车图标之前不要推送。RestaurantVC
看起来像这样
目前我正在使用 blockClosure 来传递数据
//assign item name to cell
let selectedDictName = restMenu[indexPath.row] as! NSDictionary
print("the selected dict ", selectedDictName)
cell.itemNameLabel.text = selectedDictName.value(forKey: "ItemName") as! String
// assign item price to cell
let selectedDictPrice = restMenu[indexPath.row] as! NSDictionary
cell.itemPriceLabel.text = "Price: \(selectedDictPrice.value(forKey: "ItemPrice") as! String)"
// pass ItemName and ItemPrice by blockClosure
if blockClosure != nil{
blockClosure(selectedDictName.value(forKey: "ItemName") as! String, selectedDictPrice.value(forKey: "ItemPrice") as! String )
}
最佳答案
遵循以下代码:- 使用协议(protocol)委托(delegate)
//1
//Create protocol
protocol TableViewCellDelegate {
func addTapped(cell: TableViewCell)
}
//2
//Create instance
class TableViewCell: UITableViewCell {
var delegate: TableViewCellDelegate?
….
@IBAction func addAction(_ sender: Any) {
delegate?.addTapped(cell: self)
}
}
//3
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: “cell”, for: indexPath) as! TableViewCell
cell.delegate = self
}
//4
extension ViewController: TableViewCellDelegate {
func addTapped(cell: TableViewCell) {
}
}
关于ios - 如何将值从 ViewController 传递到 UITableView 自定义单元格类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50285804/
我是一名优秀的程序员,十分优秀!