【发布时间】:2017-09-15 10:10:32
【问题描述】:
我创建了一个名为 xxxtableviewdelegate 的新类,并将 uitableviewdelgate 和 uitableviewdatasource 放入该类中,然后,我制作
tableView1?.delegate = tableView1Delegate
tableView1?.dataSource = tableView1Delegate
但是委托方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
和
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
不叫,请帮帮我
这些是详细代码
let tableView1Delegate = ArticleTableViewDelegate()
tableView1Delegate.cellIdentifier = cellIdentifier
tableView1Delegate.headerIdentifier = headerIdentifier
tableView1Delegate.sectionTitleArray = self.getSectionData()
tableView1Delegate.cellDatasource = self.getData()
tableView1 = UITableView(frame: CGRect.init(x: 0, y: 0, width: zkj_width(object: mainScrollView!), height: zkj_height(object: mainScrollView!)), style: .plain)
mainScrollView?.addSubview(tableView1!)
tableView1?.delegate = tableView1Delegate
tableView1?.dataSource = tableView1Delegate
tableView1?.separatorStyle = .none
tableView1?.register(ArticleTableViewCell.classForCoder(), forCellReuseIdentifier: cellIdentifier)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ArticleTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier!, for: indexPath) as! ArticleTableViewCell
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let aCell = cell as! ArticleTableViewCell
let model: ArticleModel = cellDatasource![indexPath.row] as! ArticleModel
aCell.setModel(articleModel: model)
}
【问题讨论】:
-
用什么方法设置委托?
-
@just_just_just 你能显示代码吗?使用扩展而不是在不同的类中重构你的表方法会更好。
-
@MaxPevsner UITableViewDelegate 和 UITableViewDataSource,有些方法有效,但这两种方法都无效
-
@just_just_just 都在这里解释-:clean-swift.com/…
-
@TusharSharma 你的意思是在扩展中实现委托吗?但这听起来很奇怪,并不是所有的方法都不起作用,只是这两个委托方法不起作用
标签: ios swift mvvm delegates tableview