OGeek|极客世界-中国程序员成长平台

标题: ios - dequeueReusableCellWithIdentifier 总是返回 nil [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:44
标题: ios - dequeueReusableCellWithIdentifier 总是返回 nil

我有一个包含 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4