经过一些重构后,我决定创建一个看起来有点像的自定义tableview:
class BaseTable: UITableView, UITableViewDelegate, UITableViewDataSource {
var rowsInSection: Int { return 0}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.delegate = self
self.dataSource = self
self.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rowsInSection
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "headerCell")!
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
然后我像这样继承它:
class CustomTable: BaseTable{
override var rowsInSection: Int {return payArray.count }
}
这样可以正常工作,但是我注意到没有一个子类的didSelectRowAt被调用了???有人可以帮忙吗?
答案 0 :(得分:1)
嗯,你应该遵循这些事情:
initWithCoder:
拆分为外部方法,并在initWithFrame:
中调用它,因为不同的方法称为不同的init
方法。如 func setUpTableView() {
self.delegate = self
self.dataSource = self
self.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
super
方法UITableView
,以及在自定义UITableViewCell
&#下实现协议的不同类39; S 答案 1 :(得分:0)
这是糟糕的设计。有UITableView
的委托和数据源协议的原因是因为视图对象应该是可重用的。您正在使BaseTable
类充当控制器和视图。
要回答你的问题,你确定它没有被调用吗?您只能deselectRow(at:animated:)
tableView(didSelectRowAt:)
来电