0
這裏的任何方法對我的類的代碼返回的cellForRowAtIndexPath錯誤:不覆蓋從超
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因爲這些方法不存在於超類中。但是,它讓我重寫numberOfRowsInSection。
爲什麼會發生這種情況,我該如何解決?
您試圖重寫舊斯威夫特2 API。 – rmaddy