xcode4 – cellForRowAtIndexPath:为通用原型单元格返回nil

这让我疯了!

我有一个通用的UITableViewController类,带有一个通用的原型单元格,标识符为“myCell”.在ARC,iOS5下开发并使用Storyboard,我使用以下方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    cell.textLabel.text = @"Title";
    return cell;
}

一切都挂在故事板,文件所有者等等.我的项目中有几个其他UITableViewController,使用相同的概念.一切正常.
这个特殊类的UITableViewController不想工作!继续向我抛出异常:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Nslog’ed – > [tableView dequeueReusableCellWithIdentifier:@“myCell”] =(null)

非常感谢任何想法和帮助!

编辑:

为简单起见:
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
返回1;
}

我已经完成的事情:
1.在故事板中删除并创建和设置UITableViewController!
2.重新启动Xcode
3.重新启动模拟器
4.重启电脑!!!!
5.一遍又一遍地删除并构建应用程序!
没有用.

顺便说一下,我试过了

if (cell == nil)
 cell = [UITableViewCell alloc] initWithStyle....

..它将解决问题,但是再次……这是ARC,故事板,……我不应该这样做. Xcode应该照顾它!

解决方法

你必须创建你的单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"] autorelease];
    }

    cell.textLabel.text = @"Title";
    return cell;
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...