解決bug:Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]

工程啟動之后,是正常顯示的,一旦上拉刷新之后就崩潰了。

以前這樣寫是沒問題的啊 = =

代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.selected) {
        
        static NSString *identifier = @"cell1";
        ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            cell = [[ChooseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.backgroundColor = [UIColor redColor];
        
        return cell;
        
    } else {
        
        static NSString *identifier = @"cell2";
        UpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            cell = [[UpTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.backgroundColor = [UIColor cyanColor];
        
        return cell;
        
    }
    
    return nil;
}

錯誤提示:

Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]

原因:

因為 cellForRowAtIndexPath 正在返回一個 nil 值,而
configureCellForDisplay 希望返回一個 UITableViewCell 值。

解決辦法:

返回一個 UITableViewCell 值即可。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // **添加一個 UITableViewCell 類型,最后返回 cell 即可。**

    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.backgroundColor = [UIColor yellowColor];
    
    
    if (self.selected) {
        
        static NSString *identifier = @"cell1";
        ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            cell = [[ChooseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.backgroundColor = [UIColor redColor];
        
        return cell;
        
    } else {
        
        static NSString *identifier = @"cell2";
        UpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            cell = [[UpTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.backgroundColor = [UIColor cyanColor];
        
        return cell;
        
    }
    
    return cell;
}

參考鏈接:

Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容