gpt4 book ai didi

swift - 当 CustomCell 标签值更改时如何更新 UITableView?

转载 作者:行者123 更新时间:2023-11-30 10:47:50 24 4
gpt4 key购买 nike

我有一个自定义单元格类,其中有两个按钮和一个在其自己的类中定义的标签。当按下按钮时,我使用协议(protocol)委托(delegate)来更新标签的值。但我无法弄清楚如何更新 UITableView。

protocol customCellDelegate: class {
func didTapDecrementBtn(_ sender: AllCountersTableViewCell)
func didTapIncrementBtn(_ sender: AllCountersTableViewCell)
func updateTableViewCell(_ sender: AllCountersTableViewCell)
}

class AllCountersTableViewCell: UITableViewCell {


@IBOutlet weak var counterValueLbl: UILabel!
@IBOutlet weak var counterNameLbl: UILabel!
@IBOutlet weak var counterResetDateLbl: UILabel!

@IBOutlet weak var counterDecrementBtn: UIButton!
@IBOutlet weak var counterIncrementBtn: UIButton!

weak var delegate: customCellDelegate?

@IBAction func decrementBtnPressed(_ sender: UIButton) {
delegate?.didTapDecrementBtn(self)
delegate?.updateTableViewCell(self)
}

@IBAction func incrementBtnPressed(_ sender: UIButton) {
delegate?.didTapIncrementBtn(self)
delegate?.updateTableViewCell(self)
}

在我的 ViewController 中,我提供了委托(delegate)。但 reloadData() 不起作用,因此尽管 sender.counterLbl.value 正在更改,但它没有反射(reflect)在 View 上。

extension AllCountersVC: customCellDelegate {

func didTapIncrementBtn(_ sender: AllCountersTableViewCell) {
guard let tappedCellIndexPath = tableView.indexPath(for: sender) else {return}
let rowNoOfCustomCell = tappedCellIndexPath[1]
let newValue = String(allCounter[rowNoOfCustomCell].incrementCounterValue(by: allCounter[rowNoOfCustomCell].counterIncrementValue))
sender.counterValueLbl.text = newValue
}

func updateTableViewCell(_ sender: AllCountersTableViewCell) {
allCountersTableView.reloadData()
}

最佳答案

cellForRow中,您必须设置cell.delegate = self此逻辑才能开始工作。仅仅让您的 Controller 符合您的自定义单元委托(delegate)协议(protocol)是不够的。从您的帖子中,我假设单元格中的 delegate 始终为 nil,这就是它不起作用的原因。

关于swift - 当 CustomCell 标签值更改时如何更新 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55388550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com