无法从其DataSource获取单元格 [英] Failed to obtain a cell from its DataSource
问题描述
以下代码有什么问题
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = @"hello";
return cell;
}
和
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
但是我无法从其dataSource获取一个单元格
整个例外是
*** 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
推荐答案
您的错误表明 cellForRowAtIndexPath
由于某种原因返回 nil
,而且我'猜测是因为你没有将可重复使用的细胞出列。如果你想确认这一点,只需在当前的出列呼叫后设置一个断点:我希望你会发现 cell
设置为 nil
。
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
.
如果您正在使用现代Xcode模板,您可以获得为您制作的原型单元格,那么您应该使用它:
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];
如果您没有使用Xcode模板,请使用该行代码然后注册您自己的代码-use标识符如下:
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"];
一切都很好,可以解决问题。 我为Swift用户更详细地写了这篇文章。
All being well that should resolve the problem. I wrote this up in more detail for Swift users.
这篇关于无法从其DataSource获取单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!