gpt4 book ai didi

objective-c - UITableView 最后两个单元格中的信息与前两个单元格重复

转载 作者:行者123 更新时间:2023-11-28 19:21:52 25 4
gpt4 key购买 nike

我有一个 UITableView,它使用以下代码从 XML 加载数据。它正确加载信息,但数组中的最后两个单元格是前两个单元格的重复。我不知道我是否加载不正确;我在构建数组时 NSLog 输出了值,它们是正确的。我还记录了最终数组,这很好。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

ViewRoutesCell * aCell = (ViewRoutesCell *)[tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"];

if (aCell == nil)
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"ViewRoutesCell" owner:self options:nil];

for (NSObject *anObj in arr) {

AssessObject *newObj1 = [[AssessObject alloc] init];
newObj1=[totalArray objectAtIndex:indexPath.row];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newObj1.routeImage]]];

aCell = (ViewRoutesCell *)anObj;
aCell.imgRoute.image = image;
aCell.lblRouteText.text = newObj1.routeText;
aCell.lblRouteImage.text = newObj1.routeImage;
}
}
return aCell;
}

它正在加载到自定义单元格。在我看来,问题可能与细胞的重用有关。有什么想法吗?

最佳答案

您必须按如下方式重写代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

ViewRoutesCell * aCell = (ViewRoutesCell *)[tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"];

if (aCell == nil)
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"ViewRoutesCell" owner:self options:nil];
aCell = (ViewRoutesCell *)[arr objectAtIndex:0];
}

AssessObject *newObj1 = [[AssessObject alloc] init];
newObj1=[totalArray objectAtIndex:indexPath.row];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newObj1.routeImage]]];

aCell.imgRoute.image = image;
aCell.lblRouteText.text = newObj1.routeText;
aCell.lblRouteImage.text = newObj1.routeImage;

return aCell;
}

否则,您的单元格将使用旧值进行缓存。

关于objective-c - UITableView 最后两个单元格中的信息与前两个单元格重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com