Zac Powell Zac Powell - 1 month ago 84
Swift Question

failed to obtain a cell from its dataSource with swift 3

I have the below UITableViewController attached as a searchResultsController:

import UIKit
import MapKit

class LocationSearchTable : UITableViewController {
var matchingItems:[MKMapItem] = []
var mapView: MKMapView? = nil

override func viewDidLoad() {
super.viewDidLoad()

tableView.dataSource = self
tableView.delegate = self
}
}

extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController){
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}

}

extension LocationSearchTable {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(matchingItems.count)
return matchingItems.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = ""
return cell
}
}


enter image description hereenter image description here

Here it crashed once
matchingItems.count
is no equal to 0 for exameple:

0
0
10
2016-10-20 15:43:53.914 ios-App[9137:977735] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:8035
2016-10-20 15:43:53.927 ios-App[9137:977735] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7fe459be7000; frame = (0 0; 768 1024); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x608001a47f20>; layer = <CALayer: 0x60800042bce0>; contentOffset: {0, -64}; contentSize: {768, 440}>) failed to obtain a cell from its dataSource


When I add debugging points witin cellForRowAtIndexPath they are never reached, the app seems to crash before this point?

Answer

The error

failed to obtain a cell from its dataSource

occurs because the signature of cellForRowAtIndexPath is wrong. In Swift 3 it's

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

and use the convenience method

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)

which returns always a valid non-optional cell.


PS: You don't need to set datasource and delegate to self because UITableViewController does that implicitly