【发布时间】: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