UITableView 数据源必须从 tableView:cellForRowAtIndexPath 错误返回一个单元格

编程入门 行业动态 更新时间:2024-10-12 01:28:46
本文介绍了UITableView 数据源必须从 tableView:cellForRowAtIndexPath 错误返回一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在这段代码中遇到了那个错误.你能帮我找出原因吗.

I am getting that error with this piece of code. Can you help me figure out why.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomerCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; Restaurant *restaurant = [self.restaurants objectAtIndex:[indexPath row]]; [[cell textLabel] setText:[NSString stringWithFormat:@"%@ %@",restaurant.Name,restaurant.Address]]; return cell; }

推荐答案

你的代码的问题是 dequeueReusableCellWithIdentifier: 并不总是返回一个有效的单元格;有时,它返回 nil.在这种情况下,您需要分配一个新的单元实例,然后以通常的方式对其进行初始化.按照目前的情况,如果从 dequeueReusableCellWithIdentifier: 方法返回一个,您的方法将返回 nil.

The problem with your code is that dequeueReusableCellWithIdentifier: does not always return you a valid cell; sometimes, it returns nil. In such cases you need to allocate a new instance of your cell, and then initialize it the usual way. As it currently stands, your method would return nil if one is returned from the dequeueReusableCellWithIdentifier: method.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyle1 reuseIdentifier:CellIdentifier]; }

更多推荐

UITableView 数据源必须从 tableView:cellForRowAtIndexPath 错误返回一个单元格

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

发布评论

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

>www.elefans.com

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