【发布时间】:2020-03-26 10:57:36
【问题描述】:
我想将选定的单元格作为文本,并将其作为字符串保存在“var productName”中。我尝试过使用 didSelectRowAt 函数,但不知道如何实现。
这是我的代码:
import UIKit
class ChooseProductViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var productName: String? = ""
var workModel: ActualValues?
var closureBlock2: (() -> Void)?
@IBOutlet weak var checkButton: UIButton!
let productList : [String] = ["Almond 20g",
"Almond 40g",
"Baharat Spice Mix 2g",
"Baharat Spice Mix 4g",
]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
myCell.textLabel?.text = productList[indexPath.row]
return myCell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func checkButton(_ sender: UIButton) {
workModel?.product = productName
closureBlock2?()
self.dismiss(animated: true, completion: nil)
}
func configureProduct(model: ActualValues) {
workModel = model
}
}
我该如何解决?
【问题讨论】:
-
要明确你的问题是“如何在 UITableView - Swift 中检测单元格选择”这是答案:[stackoverflow.com/questions/28430232/…
标签: ios swift uitableview