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



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

加载表视图时,一切看起来都很好。单击此处查看示例

但当我上下滚动到同一位置时,单元格就会变为空白,好像没有数据一样。空白单元格

如果我重复同样的过程,一切看起来都会好起来。

以下是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;
}

有什么想法吗?

在加载表视图之前,以及在用这个替换dequeueReusableCell...之后,在故事板或代码中注册你的细胞笔尖

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

删除该部分,不再需要

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

最新更新