• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - dequeueReusableCellWithIdentifier 总是返回 nil

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:44:36 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个包含 6 个单元格和几个部分的静态表格。当我初始化一个单元格时,它总是返回 nil,尽管我在传递的过程中使用了这个完全相同的方法......

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    if (indexPath.section == SAVE_SECTION) {
        ATSaveCell *cell = [tableView dequeueReusableCellWithIdentifier"SaveCell"];
        if(cell == nil) {
            NSLog(@"nil cell");
            cell = [[ATSaveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier"SaveCell"];
        }
        cell.textLabel.text = @"test";
        return cell;
    } 

nil 单元总是输出。在我的 Storyboard中,我有一个 tableviewcontroller 定义了单元格,id 是“SaveCell”。我还检查以确保表 ciew Controller 与我正在使用的类是同一个类...我在传递中使用了完全相同的方法,所以我不确定为什么单元格每次都返回 nil。

另外,初始化我的tableviewcontroller:

ATSearchSettingsViewController *mySearchSettings = [sb instantiateViewControllerWithIdentifier"SearchSettings"];

很明显,表格 View 没有在 Storyboard 中注册原型(prototype)单元格。



Best Answer-推荐答案


如果问题是 UITableView 没有从 Storyboard中取出单元格。尝试检查您使用的是 prototype cells 而不是 static cells。 UITableView 不会将静态单元格出列。

如果问题是您总是调用 [[ATSaveCell alloc] init]。如果 TableView 没有任何可重用的内容,则需要这样做。

来自文档:dequeueReusableCellWithIdentifier

This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.

因此,如果没有足够的单元格容纳 View ,它将始终创建一个新单元格。

关于ios - dequeueReusableCellWithIdentifier 总是返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23633626/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap