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

编程入门 行业动态 更新时间:2024-10-11 07:25:10
本文介绍了无法从其数据源获取单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的代码有什么问题

-(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; }

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

整个异常是

*** 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 模板,无论如何都要使用该行代码,然后像这样注册您自己的重用标识符:

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.

更多推荐

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

本文发布于:2023-06-03 23:43:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/484990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据源   单元格

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!