簡體   English   中英

斷言失敗 - [UITableView dequeueReusableCellWithIdentifier:forIndexPath:]錯誤

[英]Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] error

嘗試填充表時出現此錯誤:

*斷言失敗 - [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注冊了一個nibclass (或者您的nib / storyboardprototype cell再次使用相同的reuseIdentifier 。還保證此方法將始終返回一個有效的單元格,如果您使用dequeueReusableCellWithIdentifier:forIndexPath:方法,那么您將永遠不必以編程方式創建cell

如果您確實想以編程方式創建單元格,則應使用與if (!cell) { cell =... }代碼配對的dequeueReusableCellWithIdentifier
有關更多信息,請查看此答案

我的代碼遇到了同樣的問題。 我發現的是我不小心將UITableView的委托附加到View控制器的視圖。 將委托更改為UIViewController 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM