向上或向下滚动后,UITableView 中的自定义单元格变为空白

Custom cells in UITableView become blank after scrolling up or down

我的任务是解决应用程序中的这个错误(它用于商业用途,所以我不能 link 该项目)。我也是 Objective-C 或 IOS 开发的新手,我不知道为什么会发生以下情况。提醒一下,我正在使用自定义单元格。

加载表视图时,一切看起来都很好。 Click here to see the example

但是当我向上滚动并返回到同一位置时,单元格变为空白,就好像没有数据一样。 Blank cell

如果我重复相同的过程,一切都会恢复正常。

这是 cellForRowAtIndexPath 的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *cellId = @"cell2";
    PedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [(PedTableCell*)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
    }
    if (tableView==_table3)
    {
        
        NSString *docsDirr;
        NSString *documentsDirectoryForSaveImages;
        docsDirr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        documentsDirectoryForSaveImages = [docsDirr stringByAppendingPathComponent:@"/CarpetaImatges"];
        
        
        
        NSString *match = @".jpg";
        NSString *preCodigo;
        NSScanner *scanner = [NSScanner scannerWithString:[pedarray objectAtIndex:indexPath.row]];
        [scanner scanUpToString:match intoString:&preCodigo];

        NSString *nomimg=[NSString stringWithFormat:@"%@.jpg", preCodigo];
        
        UIImage *img = [UIImage imageWithContentsOfFile:[documentsDirectoryForSaveImages stringByAppendingPathComponent:nomimg]];
        
        cell.img.image = img;
        cell.layer.borderWidth=1.0f;
        cell.img.layer.borderWidth=1.0f;
        
        
        
        
        cell.cart.text = preCodigo;
        cell.descart.text = [nomarray objectAtIndex:indexPath.row];
        cell.cant.text = [cantarray objectAtIndex:indexPath.row];
        cell.precart.text = [precioarray objectAtIndex:indexPath.row];
        cell.pvpcart.text = [pvparray objectAtIndex:indexPath.row];
        
        [cell layoutIfNeeded];
        
    }
    return cell;
    
}

我已经调试了该方法,每次它 returns 都是一个有效的单元格。当我单击空单元格时,它仍然按预期工作。本质上,数据就在那里,只是由于某种原因没有呈现。

我已经检查了其他有 same/similar 问题的帖子,但似乎没有什么真正匹配。 PrepareForReuse 是行不通的,因为没有要更改的 UI,只有内容。我正在使用 indexPath.row 获取数组的值,以确保获取正确的值。我什至尝试将 heightForRowAtIndexPath 设置为单元格应具有的相同高度(所有单元格的大小相同且永远不会改变)或让 AutoLayout 处理它但无济于事。

numberOfRowsInSection 的代码(Returns 当前数据库中存在的订单数量):

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView == _table3)
    {
        [self cargacarro];
        
        return pedarray.count;
        
    }
    return pedarray.count;
}

有什么想法吗?

在加载 tableview 之前在故事板或代码中注册您的单元格笔尖,然后用这个

替换 dequeueReusableCell...
PedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];

cell.img... 
cell.cart...

删除这部分,不再需要

if (cell == nil)
{
    cell = [(PedTableCell*)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}