Ben Beatles Levine
Ben Beatles Levine

Reputation: 153

Overriding cellForRowAtIndexPath in UITableViewController subclass returns error: does not override any method from superclass

Here is the code for my 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
    }

}

Even though VideoSearchController inherits from UITableViewController, it won't let me override cellForrowAtIndexPath or numberOfSectionsInTableView because those methods don't exist in the superclass. However, it does let me override numberOfRowsInSection.

Why is this happening and how do I fix it?

Upvotes: 0

Views: 597

Answers (1)

Shades
Shades

Reputation: 5616

The declarations are:

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

And

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

Upvotes: 2

Related Questions