gpt4 book ai didi

ios - dequeueReusableCellWithIdentifier 总是返回 nil

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:43:09 25 4
gpt4 key购买 nike

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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)单元格。

最佳答案

如果问题是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com