Failed to obtain a cell from its DataSource
Your error suggests that cellForRowAtIndexPath
is returning nil
for some reason, and I'm guessing it's because you are failing to dequeue a reusable cell. If you want to confirm this, just set a breakpoint after your current dequeue call: I expect you'll find cell
is set to nil
.
If you're using modern Xcode templates where you get a prototype cell made for you, you should probably be using this instead:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
If you aren't using an Xcode template, use that line of code anyway then register your own re-use identifier like this:
[self.tableView registerClass:[UITableViewCell self] forCellReuseIdentifier:@"Cell"];
All being well that should resolve the problem. I wrote this up in more detail for Swift users.
failed to obtain a cell from its dataSource with swift 3
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
UITableview: failed to obtain a cell from its dataSource
Cross check below check list:-
- it's because you are failing to dequeue a reusable cell.
- The problem is that your cellForRowAtIndexPath function is embedded in another function
- when you forgot to add the UITableViewDataSource and UITableViewDelegate protocols to the
ViewController declaration
.
class DataViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
failed to obtain a cell from its dataSource for swift4.2
Can you print weeklyWeather.count to see its number?
You may also peek the cell by setting a breakpoint at beginning of your tableView: cellForRowAt method.
Hope it helps.
UITableView' failed to obtain a cell from its dataSource
Your method name for cellForRow is not correct.
Replace this line
func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
{
with
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Related Topics
How to Save a Custom Class as an Attribute of a Coredata Entity in Swift 3
Images Inaccessible from Asset Catalog in a Swiftui Framework
Cannot Invoke Initializer for Type 'Sqlite3_Destructor_Type'
Count the Number of Lines in a Swift String
Why Use Float(Arc4Random())/0Xffffffff Instead of Drand()
Perform Migration by Adding List() and Another Model Class
Diffrence Between Function and Generic Function in Swift
Create Complicated Nscompoundpredicate in Swift 3
Problem with Gesture in Xcode 12 and iOS 14
How to Add Icon to a Share Sheet in Swift
How to Refresh Multiple Timers in Widget iOS14
Possible to Write Swift Println Logs into File Too
Swiftui: How to Only Run Code When the User Stops Typing in a Textfield
Accessing Global Variable in Swift
Querying in Firebase by Child of Child