我有一个带有自定义单元格的 tableView,每当我单击复选框按钮时,单元格内的值都会增加,即单元格中的(0 到 1),并且在取消选中时值会减少,这非常有效。但是每当我尝试将这些值从单元格打印到 tableView 外部的 UILabel 时,这些值都没有改变。
这是我用过的下面的代码
var data = [[String: AnyObject]]()
func getDetails() {
let paymentURL = paymentListURL + String(28) + "&student_id=" + String(33)
Alamofire.request(paymentURL).responseJSON { (response) in
if ((response.result.value) != nil) {
var jsonVar = JSON(response.result.value!)
print(jsonVar)
if let da = jsonVar["types"].arrayObject {
self.data = da as! [[String:AnyObject]]
}
if self.data.count > 0 {
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TabCell
cell.checkB.tag = indexPath.row
let ip = data[indexPath.row]
cell.nameText.text = ip["title"] as? String
if cell.nameText.text == "Mandatory testing" {
cell.checkB.checkState = .checked
cell.backgroundColor = UIColor.lightGray
cell.checkB.backgroundColor = UIColor.lightGray
}
if ip["mandatory"] as? String == "yes" {
moneyText.text = ip["amount"] as? String
//moneyText is UILabel outside Tableview
cell.amountValue.text = ip["amount"] as? String
cell.checkB.isEnabled = false
} else {
moneyText.text = "0"
if cell.amountValue.text == "1"{
print("ONE")
}
}
return cell
}
func didPressButton(_ tag: Int) {
let indexPath = IndexPath.init(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! TabCell
moneyText.text = String(cell.someValue)
}
对于我使用的 TableviewCell
protocol TabCellDelegate {
func didPressButton(_ tag: Int)
}
class TabCell: UITableViewCell {
@IBOutlet weak var checkB: M13Checkbox!
@IBOutlet weak var nameText: UILabel!
@IBOutlet weak var amountValue: UILabel!
var someValue: Int = 0 {
didSet {
amountValue.text = "\(someValue)"
}
}
@IBAction func checkBAction(_ sender: M13Checkbox) {
cellDelegate?.didPressButton(sender.tag)
if checkB.checkState == .checked {
someValue += 1
} else if checkB.checkState == .unchecked {
someValue -= 1
}
}
}
我尝试首先将这些值从单元格添加到数组,然后将所有值添加到数组中并在 UILabel 中打印,但值没有改变,它只是递增。即,即使在取消选中复选框后,该值也是增加。
我什至尝试使用它对我不起作用的协议(protocol)
任何帮助将不胜感激。
最佳答案
您正在从 TabCell
内的 checkBAction
处理程序更新 someValue
。然后,属性 didSet
处理程序将更新 amountValue
标签。这就是更新单元格标签的原因。
在 someValue
更改后,您没有任何更新 moneyText
的代码。您只从 tableView(_:cellForRow:)
设置 moneyText.text
,但是当显示单元格时会调用它,可能在滚动后多次调用等。
您可以执行以下操作:
- 在单元格内创建一个委托(delegate)属性(使用自定义协议(protocol)作为类型)
- 将 Controller 设置为该委托(delegate)
- 当值改变时,调用该委托(delegate)的函数(例如调用 Controller )
- 在里面,更新
moneyText
作为一个想法(可能无法编译,因为我没有你所有的类):
protocol MyTabCellProtocol {
func checkboxChanged(_ checkbox:M13Checkbox, atRow:Integer)
}
class TabCell: UITableViewCell {
weak delegate:MyTabCellProtocol?
// ...
@IBAction
func checkBAction(_ sender: M13Checkbox) {
if checkB.checkState == .checked {
someValue += 1
} else if checkB.checkState == .unchecked {
someValue -= 1
}
delegate?.checkboxChanged(self, checkB.tag)
}
}
class MyController : UIViewController, MyTabCellProtocol {
func checkboxChanged(_ checkbox:M13Checkbox, atRow:Integer) {
moneyText.text = "\(checkbox.someValue)"
}
}
但如果我进一步考虑,我建议稍微重构一下整个代码。我看到的问题是您在单元格内的操作处理程序确实更新了单元格的 someValue
属性,但没有更新外部模型 (ip["amount"]
) .我认为你应该做的是:
- 在单元格的
checkBAction
中,只需调用委托(delegate)并提供有关已修改行的信息 (self.checkB.tag
) 和检查状态。 不要在此处更新amountValue
! - 在委托(delegate)实现中,更新模型
ip["amount"]
- 调用 TableView 的
reloadRows(at:with:)
刷新cell - 然后,将自动调用
cellForRow
,然后您可以在其中更新单元格和外部标签。
关于swift - 如何从 TableView 的值动态打印 UILabel 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55294276/