failed to obtain a cell from its dataSource with swift 3

前端 未结 2 656
清歌不尽
清歌不尽 2020-12-11 23:51

I have the below UITableViewController attached as a searchResultsController:

import UIKit
import MapKit

class LocationSearchTable : UITableViewController {         


        
相关标签:
2条回答
  • 2020-12-12 00:22

    I had similar issue! After several minutes of debugging, I realized I didn't inherit the

    UITableViewDelegate, UITableViewDataSource

    on my ViewController.

    0 讨论(0)
  • 2020-12-12 00:23

    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

    0 讨论(0)
提交回复
热议问题