ios 无法从其数据源获取单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34250595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:22:44  来源:igfitidea点击:

Failed to obtain a cell from its DataSource

iosobjective-cuitableview

提问by Rishab

What is wrong with the below code

下面的代码有什么问题

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CellIdentifier  = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell.textLabel.text = @"hello";

    return cell;
}

And

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

but I'm getting failed to obtain a cell from its dataSource

但我无法从其数据源获取单元格

The entire exception is

整个例外是

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7ffe0b092800; frame = (0 97; 414 590); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ffe0acf3b10>; layer = <CALayer: 0x7ffe0aceb6e0>; contentOffset: {0, 0}; contentSize: {414, 88}>) failed to obtain a cell from its dataSource (<AllContactsViewController: 0x7ffe0ace60f0>)'

I have set delegate and data source for the table view

我已经为表视图设置了委托和数据源

回答by David Reidy

Using Swift 3 I encountered this error when I forgot to add the UITableViewDataSource and UITableViewDelegate protocols to the ViewController declaration.

当我忘记将 UITableViewDataSource 和 UITableViewDelegate 协议添加到 ViewController 声明时,使用 Swift 3 时遇到了这个错误。

class DataViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

类 DataViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

Adding them fixed the problem.

添加它们解决了问题。

回答by TwoStraws

Your error suggests that cellForRowAtIndexPathis returning nilfor 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 cellis set to nil.

您的错误表明由于某种原因cellForRowAtIndexPath正在返回nil,我猜这是因为您未能将可重用单元出列。如果您想确认这一点,只需在您当前的出队调用后设置一个断点:我希望您会发现cell设置为nil.

If you're using modern Xcode templates where you get a prototype cell made for you, you should probably be using this instead:

如果您使用的是现代 Xcode 模板,您可以在其中获得为您制作的原型单元格,您可能应该使用它:

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:

如果您没有使用 Xcode 模板,请使用该行代码,然后注册您自己的重用标识符,如下所示:

[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.

一切都很好,应该可以解决问题。我为 Swift 用户更详细地写了这篇文章。

回答by Vivek

Swift 3.0

斯威夫特 3.0

You just need to add UITableViewDelegateand UITableViewDataSource

你只需要添加UITableViewDelegateUITableViewDataSource

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

Below image is just for your reference.

下图仅供参考。

enter image description here

在此处输入图片说明

回答by Muhammad Zohaib Ehsan

You have to give the cell an identifier. "Cell" give to the identifier of cell as in the attributes inspector of cell in the identifier field. I re-produced your error and it is due to you have not given an identifier to your cell.

您必须为单元格提供一个标识符。“单元格”赋予单元格标识符,如标识符字段中单元格的属性检查器。我重现了你的错误,这是因为你没有给你的单元格提供标识符。

回答by Mohsen Hossein pour

I had the same issue and it was because I forgot to implement UITableViewDataSource and UITableViewDelegate protocols

我有同样的问题,那是因为我忘记实现 UITableViewDataSource 和 UITableViewDelegate 协议

just add them to your class declaration after the inherited class

只需将它们添加到继承类之后的类声明中

xcode must warn me about that, because I used UITableViewDataSource and UITableViewDelegate methods in my code

xcode 必须警告我这一点,因为我在代码中使用了 UITableViewDataSource 和 UITableViewDelegate 方法

回答by prakash singh

In my case, I was forgot to declare UITableViewDataSource, UITableViewDelegate for UITableview.

就我而言,我忘记为 UITableview 声明 UITableViewDataSource、UITableViewDelegate。

回答by sarah

in my case, I forgot to change from dynamic prototype to static cell, check this part in your interface builder

就我而言,我忘记从动态原型更改为静态单元格,请在界面构建器中检查此部分

回答by Mehdi

Careful when you are migrating
In case anyone is migrating their code from older swift versions to newer ones, this may occur if you unintentionally don't change UITableViewDataSourcefunctions' syntax to the newer swift syntax.
Unfortunately the compiler won't complain about it.
I faced this when I was migrating my code from swift 2 to swift 4. I didn't change cellForRow DataSource method syntax from:

迁移时要小心
如果有人将他们的代码从较旧的 swift 版本迁移到较新的版本,如果您无意中没有将UITableViewDataSource函数的语法更改为较新的 swift 语法,则可能会发生这种情况。
不幸的是,编译器不会抱怨它。
当我将代码从 swift 2 迁移到 swift 4 时,我遇到了这个问题。我没有将 cellForRow DataSource 方法的语法从:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell   

to its swift 4 version:

到其 swift 4 版本:

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

and I got the error:

我得到了错误:

"Failed to obtain a cell from its DataSource"

回答by Urvish Modi

Check if have added UITableViewDataSource were added. I missed this, so that app was crashing

检查是否添加了 UITableViewDataSource。我错过了这个,所以那个应用程序崩溃了

回答by Mohsin Qureshi

Make sure you have not forgotten to implement the UITableViewDelegate and UITableViewDataSource Protocols.

确保你没有忘记实现 UITableViewDelegate 和 UITableViewDataSource 协议。

class ExampleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

class ExampleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource