无法覆盖UITableViewDataSource和UITableViewDelegate
问题描述:
extension FormViewController : UITableViewDelegate {
public func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
}
extension FormViewController : UITableViewDataSource {
public func numberOfSectionsInTableView(tableView: UITableView) -> Int
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?
}
class NotifiableFormViewController: FormViewController
为什么我不能实现和重写DataSource和TableViewDelegate?错误:“方法不覆盖它的任何超类方法”无法覆盖UITableViewDataSource和UITableViewDelegate
class FriendsTableViewController: NotifiableFormViewController{
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
}
答
根据定义,扩展只能将功能添加到现有类。它不能改变,包括重写,现有的实现。我希望这有帮助。
答
更简单,删除该单词override
因为你没有覆盖方法,要实现它
答
UITableViewDataSource
和UITableViewDelegate
只是协议 如果你想覆盖它,你在NotifiableFormViewController
然后在你的覆盖FriendsTableViewController
但如果没有在你的超类实现,那么你不能覆盖它(没有覆盖实现的委托或数据源函数)
可能是这可以帮助:http://*.com/a/31432610/656600 – rptwsthi
我需要在扩展名 – UnRewa
[重写方法在Swift扩展]可能的重复(http://*.com/questions/38213286 /压倒一切的方法合迅速的扩展) – Koraktor