gpt4 book ai didi

iphone - 滚动 UITableView 时单元格图像发生变化

转载 作者:行者123 更新时间:2023-12-03 21:02:00 25 4
gpt4 key购买 nike

嗨,我的问题是,当我滚动 UITableView 单元格图像时,这些图像实际上位于网络服务器上,并且我使用 asyncdownloading 下载它。我在 UITableViewCell 上添加了 UIImageView 。图像在 UITableViewCell 上成功显示,但是当我滚动 tableview 图像时,这就是代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];
searchobjectval =[self.arrSearchResult objectAtIndex:indexPath.row];
if(cell == nil)
{
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DefaultCell"];



}

UILabel *areaLbl = (UILabel *)[cell viewWithTag:1];
UILabel *postTitle = (UILabel *)[cell viewWithTag:2];
UILabel *roomCost = (UILabel *)[cell viewWithTag:3];
UILabel *description = (UILabel *)[cell viewWithTag:4];
areaLbl.text =searchobjectval.location;
postTitle.text = searchobjectval.title;
roomCost.text = searchobjectval.roomCost;
description.text =searchobjectval.description;

[tableView setSeparatorColor:[UIColor blueColor]];

UIView *myView = [[UIView alloc] init];
if (indexPath.row % 2)
{
UIColor* clr = [UIColor colorWithRed:0.4f green:0.6f blue:0.8f alpha:1];
myView.backgroundColor = clr;
}
else
{
myView.backgroundColor = [UIColor whiteColor];
}
cell.backgroundView = myView;

NSLog(@" search object image %@",searchobjectval.imgLink);


// Pass along the URL to the image (or change it if you are loading there locally)
[AsyncImagesDownloading processImageDataWithURLString:[NSString stringWithFormat: @"http://192.95.48.72/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] andBlock:^(NSData *imageData)
{
if (self.view.window)
{
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10];
imgVw.image = image;
}
}];




return cell;
}

最佳答案

出于内存管理目的,UITableView 重用其单元格 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"]; 并在向上或向下滚动时加载单元格内容。如果您不将数据提取到单元格的元素中,那么当您滚动回该单元格时,您将获得上次使用该单元格时提取的最后一个值。

就您而言:

// Pass along the URL to the image (or change it if you are loading there locally)
[AsyncImagesDownloading processImageDataWithURLString:[NSString stringWithFormat: @"http://192.95.48.72/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] andBlock:^(NSData *imageData)
{
if (self.view.window)
{
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10];
imgVw.image = image;
}
}];

因此,当 !self.view.window 时,您将获得最后一次使用单元格的图像(if 子句返回 TRUE),并且它会在滚动时发生变化。您需要做的是:

// Pass along the URL to the image (or change it if you are loading there locally)
[AsyncImagesDownloading processImageDataWithURLString:[NSString stringWithFormat: @"http://192.95.48.72/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] andBlock:^(NSData *imageData)
{
if (self.view.window)
{
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10];
imgVw.image = image;

} else {

UIImage *image = [UIImage imageNamed:@"placeHolder.png"];
UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10];
imgVw.image = image;

}

}];

希望这是明确有用的:)

关于iphone - 滚动 UITableView 时单元格图像发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523735/

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