gpt4 book ai didi

ios - -[UITableView dequeueReusableCellWithIdentifier :forIndexPath:] error 断言失败

转载 作者:行者123 更新时间:2023-11-28 22:12:29 25 4
gpt4 key购买 nike

我在尝试填充我的表时遇到此错误:

* Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *dictionary = tasks[indexPath.row];

if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = dictionary[@"key"];
cell.detailTextLabel.text = [Global getPriority:(NSString *)dictionary[@"key_2"]];

return cell;
}

当我删除 forIndexPath 时,错误消失了,但随后我可以使用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法。

有人可以帮帮我吗?

此外,该表是以编程方式构建的。

谢谢,

彼得

最佳答案

dequeueReusableCellWithIdentifier:forIndexPath: 期望您之前已经为相同的 reuseIdentifier 注册了一个 nib 或一个 class (或者您的 nib/storyboard 中有一个 prototype cell 再次具有相同的 reuseIdentifier。它也是保证此方法将始终返回一个有效的单元格,因此如果您使用 dequeueReusableCellWithIdentifier:forIndexPath: 方法,您将永远不必以编程方式创建 cell

如果您确实想以编程方式创建单元格,那么您应该将 dequeueReusableCellWithIdentifier 与您的 if (!cell) { cell =... } 代码结合使用。< br/>有关更多信息,请查看 this answer

关于ios - -[UITableView dequeueReusableCellWithIdentifier :forIndexPath:] error 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22543483/

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