我正在嘗試使用委托從我的 VC 更新我的所有單元格。
它可以從單元格更新 VC 上的某些內容,但是當我嘗試從 VC 更新某些內容以對單元格進行更改時,它不起作用。
在我的 VC 中,我添加了:
protocol MainViewDelegate {
func updateValueInCell()
}
var delegate: MainViewDelegate?
@IBAction func modifyValueAtVC(_ sender: Any) {
func updateValueInCell()
}
在我的牢房里
var mainView = ViewController()
extension CustomTableViewCell : MainViewDelegate {
func updateValueInCell() {
print("hello")
}
override func awakeFromNib() {
super.awakeFromNib()
mainView.delegate = self
}
但是當我從視圖控制器按下按鈕時什么也沒發生。
這是從VC更新單元格的正確方法還是你們有其他想法?
謝謝!:)
uj5u.com熱心網友回復:
vc更新單元只需要reloadData,如果你想通過單元觸發視圖控制器,使用委托。
class MainView:UIViewController,CustomTableViewCellDelegate{
@IBOutlet weak var table: UITableView!//table.delegate = self
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DailyScheduleTableViewCell
cell.delegate = self
//update what you want
return cell
}
func onClick(cell: CustomTableViewCell) {
//cell click trigger
}
@IBAction func modifyValueAtVC(_ sender: Any) {
table.reloadData()
}
}
protocol CustomTableViewCellDelegate{
func onClick(cell:CustomTableViewCell)
}
class CustomTableViewCell:UITableViewCell{
var delegate:CustomTableViewCellDelegate!
@IBAction func onClick(_ sender: Any) {
del?.onClick(cell: self)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415449.html
標籤:
