ios - 覆盖 UITableViewController 子类中的 cellForRowAtIndexPath 返回错误 : does not override any method from superclass

标签 ios swift class inheritance overriding

这是我类(class)的代码

import UIKit

class VideoSearchController: UITableViewController, UISearchResultsUpdating {

    var searchResults: NSDictionary = [:]

    let searchController = UISearchController(searchResultsController: nil)

    // Can't override
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! SearchTableCell

        cell.videoTitle.text = "This is the title"
        cell.viewCount.text = "1,234,567"
        cell.channelName.text = "benlvn"
        let url = URL(string: "https://i.redd.it/78r3ttko3nnz.jpg")
        let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
        cell.thumbnail.image = UIImage(data: data!)

        return cell
    }

    // Can't override
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    // Override works
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func updateSearchResults(for searchController: UISearchController) {
        // TODO
    }

    func searchBarIsEmpty() -> Bool {
        // Returns true if the text is empty or nil
        return searchController.searchBar.text?.isEmpty ?? true
    }

}

即使 VideoSearchController 继承自 UITableViewController,它也不会让我覆盖 cellForrowAtIndexPath 或 numberOfSectionsInTableView,因为这些方法不存在于父类(super class)中。但是,它确实让我可以覆盖 numberOfRowsInSection。

为什么会发生这种情况,我该如何解决?

最佳答案

声明是:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

override func numberOfSections(in tableView: UITableView) -> Int {

关于ios - 覆盖 UITableViewController 子类中的 cellForRowAtIndexPath 返回错误 : does not override any method from superclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46384037/

相关文章:

java - 使用多个类的 Bukkit

java - 在外部类中使用微调器,因为所有其他 Activity 都需要

iphone - MKStoreKit 编译错误

ios - 如何删除没有对象和使用 block 定义的观察者?

swift - 在 IOS 10.0 或更高版本上本地化 Emoji 的阅读

arrays - 我如何确保用户不能重复输入?

ios - 自定义形状 UIButton

ios - Json 数据和我所做的类不匹配

ios - swift 3 如何获取明天和昨天的日期(注意特殊情况)新月或新年

c++ - 如何包装另一个类(C++)