【问题标题】:Custom cells in UITableView become blank after scrolling up or downUITableView 中的自定义单元格在向上或向下滚动后变为空白
【发布时间】:2021-08-01 18:28:18
【问题描述】:

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

当 tableview 加载时,一切看起来都很好。 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;
    
}

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

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

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

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

有什么想法吗?

【问题讨论】:

  • 表格视图单元格数据依赖于nomarray、cantarray、precioarray、pvparray?恐怕cellForRowAtIndexPath委托方法中显示的代码没有意义。那么numberOfRowsInSection 委托方法返回什么?
  • @ElTomato numberOfRowsInSection 返回 pedarray 的计数值。
  • 那么在设置表格单元格数据的时候应该坚持cellForRowAtIndexPath委托方法中的pedarray。
  • @ElTomato 事情是这样的,在 cargaCarro 上,从数据库中检索对象,但值被保存到不同的数组中,pedarrays 有所有订单代码,precioarray 所有价格等。Pedarray[0]: " Order 1" precioarray[0]: "price of Order 1" 等。重要的是行索引,正如我所说,加载的数据是正确的并且与预期值匹配,它只是没有渲染。

标签: ios objective-c uitableview


【解决方案1】:

在加载表格视图之前在情节提要或代码中注册您的单元格笔尖,并在将 dequeueReusableCell... 替换为此之后

PedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];

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

删除这部分,不再需要

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

【讨论】:

  • 我接受您的回答,因为它实际上解决了我的问题。我必须先弄清楚我需要一个 .xib 文件,但现在不是显示数据,而是显示我给标签的名称。我是不是忘记在 xib 上设置一些东西了?
  • 这里cell = [(PedTableCell*)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];你分配了一个默认样式的新单元格,这不是你的单元格,它是一个默认的,如果你使用.xib,你需要注册你的笔尖或直接添加它如果您使用的是故事板,则为 tableview,然后只需调用 [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath]; 它就会返回您的单元格。
  • 我忘记将 xib 的出口链接到 .h 属性。现在一切都很好。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2017-01-08
相关资源
最近更新 更多