gpt4 book ai didi

ios - 未调用委托(delegate)方法并给出 nil 值

转载 作者:行者123 更新时间:2023-11-28 13:41:32 25 4
gpt4 key购买 nike

由于某种原因,委托(delegate)方法没有被调用。我在控制台中打印委托(delegate)属性,但它返回 nil。我一直在寻找其他答案,但没有一个对我有帮助。

protocol FilterSwitchDelegate {
func switchValueChanged(tagValue:Int)
}

class FilterTableViewCell: UITableViewCell {
@IBOutlet weak var filterLabel: UILabel!
@IBOutlet weak var filterSwitch: UISwitch!

var delegate:FilterSwitchDelegate?

@IBAction func filterSwitchValueChanged(_ sender: UISwitch) {
print(sender.tag)

delegate?.switchValueChanged(tagValue: sender.tag)
}
}

//在FilterViewController中我调用了这个委托(delegate)方法

class FilterViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!

var dataSource = FilterTableViewCell()
var filterModel = [FilterVCDataModel]()
var ids = [Int]()

override func viewDidLoad() {
super.viewDidLoad()
dataSource.delegate = self
}
}

extension FilterViewController: FilterSwitchDelegate {
func switchValueChanged(tagValue: Int) {
ids.remove(at: tagValue)
print(ids)
}
}

TableView 数据源和委托(delegate)方法

extension FilterViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filterModel.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FilterTableViewCell
cell.filterSwitch.tag = indexPath.row
cell.filterLabel.text = filterModel[indexPath.row].filterData["title"] as? String
return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 66
}
}

最佳答案

cellForRow 中,您必须使您的单元格出队。

let cell = tableView.dequeueReusableCell(withIdentifier: "yourId") as? FilterTableViewCell

cell.delegate = self

return cell

任务完成

关于ios - 未调用委托(delegate)方法并给出 nil 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55955246/

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