【发布时间】:2019-03-28 00:41:49
【问题描述】:
我有一个自定义单元格类,其中有两个按钮和一个在其自己的类中定义的标签。按下按钮时,我正在使用协议委托来更新标签的值。但我无法弄清楚如何更新 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 中,我提供了委托。但是 reloadData() 不起作用,所以尽管sender.counterLbl.value 正在改变它并没有反映在视图上。
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()
}
【问题讨论】:
标签: swift uitableview