ios - 如何解析从 UITableViewCell 到主视图控制器的值(步进器)?
问题描述
我有一个UITableViewCell
和一个UIViewController
。在UITableViewCell
我有一个步进器。当用户单击步进器将值发送到主视图控制器并在我的表格视图单元格中接收它时,我该如何做到这一点?
我试图从单元格中的步进器获取值,但它不起作用。我的代码如下。
第一个:UITableViewCell
import UIKit
import HCSStarRatingView
import GMStepper
class FoodsSecoundTableViewCell: UITableViewCell {
@IBOutlet weak var foodTitle: UILabel!
@IBOutlet weak var foodPrice: UILabel!
@IBOutlet weak var foodRating: HCSStarRatingView!
@IBOutlet weak var foodImage: UIImageView!
@IBOutlet weak var steperCount: GMStepper!
var result : Double?
override func awakeFromNib() {
super.awakeFromNib()
print(steperCount.value) // this line has print the value just one time
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
@IBAction func myStepper(_ sender: Any) { // its a function
}
}
二:主视图控制器
import UIKit
class FoodsViewController: UIViewController , UITableViewDataSource {
var foods = [Foods]()
@IBOutlet weak var myTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
foods = [Foods(name: "myData", price: 15, count: 0, description: "myData", time: "20 - 30 min", rating: 4, image: #imageLiteral(resourceName: "chick")),
Foods(name: "myData", price: 30, count: 0,description: "myData", time: "20 - 30 min", rating: 5, image: #imageLiteral(resourceName: "chick")),
Foods(name: "myData", price: 20,count: 0,description: "myData", time: "20 - 30 min", rating: 3, image: #imageLiteral(resourceName: "chick")),
Foods(name: "myData", price: 40,count: 0, description: "myData", time: "20 - 30 min", rating: 5, image: #imageLiteral(resourceName: "chick")),
Foods(name: "myData", price: 55, count: 0,description: "myData", time: "20 - 30 min", rating: 4, image: #imageLiteral(resourceName: "chick"))
]
myTable.rowHeight = 171
myTable.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foods.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let food = foods[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "foods") as! FoodsSecoundTableViewCell
cell.foodImage.image = food.image
cell.foodPrice.text = String(food.price)
print(cell.steperCount.value) // the result hear : its print just for one time
cell.foodTitle.text = food.name
cell.foodRating.value = CGFloat(food.rating)
return cell
}
}
解决方案
您可以查看GMStepper的示例代码。
在FoodsViewController
中,当您创建单元格时,将回调添加到steperCount
cell.steperCount.addTarget(self, action: #selector(FoodsViewController.stepperValueChanged), for: .valueChanged)
添加回调函数:
@objc func stepperValueChanged(stepper: GMStepper) {
print(stepper.value, terminator: "")
}
推荐阅读
- flutter - 使这个 Tabbar 颤动
- python - Pandas 解析经过两次 html 编码的 xml
- angular - 将base64数组发送到json
- javascript - Vanilla JS根据鼠标位置滚动项目列表
- android-webview - 为什么在 Android Chrome 上使用前置摄像头时,我无法从后置摄像头获取 MediaStream?
- spring - 将版本从 2.3.8 更新到 2.5.6 时 Spring boot applicationcontext null
- python - 打印几个 CSV 文件以安装到 Postgres - 将 Float 转换为 Integer 时出错
- go - Golang ethclient.Client - 如何进行 RPC 调用?
- java - 使用java向具有国际字符的收件人发送电子邮件
- javascript - 我如何阻止 Parent Div 因为它的孩子而扩展?