gpt4 book ai didi

ios - 快速滚动 UITableView 的错误图像

转载 作者:行者123 更新时间:2023-11-28 18:33:23 25 4
gpt4 key购买 nike

我有一个提供产品信息的应用程序。资料(含图片)来源于网络。

它工作正常,但当我快速滚动时,我会看到错误的图片,直到加载正确的图片。当用户快速滚动时,如何在加载图像之前显示空白的 UIImage?

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";

CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (Cell == nil) {
Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *titeltext = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSString *oudprijstext = [[stories objectAtIndex: storyIndex] objectForKey:@"prijsoud"];
NSString *nieuwprijstext = [[stories objectAtIndex: storyIndex] objectForKey:@"prijsnieuw"];
NSString *tijdtext = [[stories objectAtIndex: storyIndex] objectForKey:@"verloopt"];
NSString *plaatjetext = [[stories objectAtIndex: storyIndex] objectForKey:@"thumb"];
NSString *linktext = [[stories objectAtIndex: storyIndex] objectForKey:@"url"];
NSString *buttontext = [[stories objectAtIndex: storyIndex] objectForKey:@"knop"];
NSString *aanbiedertext = [[stories objectAtIndex: storyIndex] objectForKey:@"logo"];

plaatjetext = [plaatjetext stringByReplacingOccurrencesOfString:@"<![CDATA[" withString:@""];
plaatjetext = [plaatjetext stringByReplacingOccurrencesOfString:@"]]>" withString:@""];
plaatjetext = [plaatjetext stringByReplacingOccurrencesOfString:@" " withString:@""];
plaatjetext = [plaatjetext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

aanbiedertext = [aanbiedertext stringByReplacingOccurrencesOfString:@"<![CDATA[" withString:@""];
aanbiedertext = [aanbiedertext stringByReplacingOccurrencesOfString:@"]]>" withString:@""];
aanbiedertext = [aanbiedertext stringByReplacingOccurrencesOfString:@" " withString:@""];
aanbiedertext = [aanbiedertext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

UIFont *titelfont = [UIFont fontWithName:@"TitilliumWeb-SemiBold" size:12];
UIFont *oudfont = [UIFont fontWithName:@"TitilliumWeb-Light" size:12];
UIFont *buttonfont = [UIFont fontWithName:@"TitilliumWeb-SemiBold" size:15];

[Cell.titel setText:titeltext];
Cell.titel.lineBreakMode = UILineBreakModeWordWrap;
Cell.titel.numberOfLines = 2;
Cell.titel.font = titelfont;
[Cell.tijd setText:tijdtext];
Cell.tijd.font = titelfont;
[Cell.knop setText:buttontext];
[Cell.knop setFont:buttonfont];
[Cell.prijsnieuw setText:[NSString stringWithFormat:@"€ %@", nieuwprijstext]];
Cell.prijsnieuw.font = titelfont;
[Cell.prijsoud setText:[NSString stringWithFormat:@"€ %@", oudprijstext]];
Cell.prijsoud.font = oudfont;

UIImage *cachedImage = [self.imageCache objectForKey:plaatjetext];
if (cachedImage)
{
Cell.plaatje.image = cachedImage;
}
else
{
// you'll want to initialize the image with some blank image as a placeholder

//cell.imageView.image = [UIImage imageNamed:@"blankthumbnail.png"];

// now download in the image in the background

[self.imageDownloadingQueue addOperationWithBlock:^{

NSURL *imageUrl = [NSURL URLWithString:plaatjetext];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = nil;
if (imageData)
image = [UIImage imageWithData:imageData];

if (image)
{
// add the image to your cache

[self.imageCache setObject:image forKey:plaatjetext];

// finally, update the user interface in the main queue

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible

UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
Cell.plaatje.image = image;
}];
}
}];
}

UIImage *cachedImage2 = [self.imageCache2 objectForKey:aanbiedertext];
if (cachedImage2)
{
Cell.aanbieder.image = cachedImage2;
}
else
{
// you'll want to initialize the image with some blank image as a placeholder

//cell.imageView.image = [UIImage imageNamed:@"blankthumbnail.png"];

// now download in the image in the background

[self.imageDownloadingQueue2 addOperationWithBlock:^{

NSURL *imageUrl2 = [NSURL URLWithString:aanbiedertext];
NSData *imageData2 = [NSData dataWithContentsOfURL:imageUrl2];
UIImage *image2 = nil;
if (imageData2)
image2 = [UIImage imageWithData:imageData2];

if (image2)
{
// add the image to your cache

[self.imageCache2 setObject:image2 forKey:aanbiedertext];

// finally, update the user interface in the main queue

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible

UITableViewCell *updateCell2 = [tableView cellForRowAtIndexPath:indexPath];
if (updateCell2)
Cell.aanbieder.image = image2;
}];
}
}];
}
//Cell.plaatje.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:plaatjetext]]];
//Cell.aanbieder.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:aanbiedertext]]];

return Cell;
}

请帮助我提供说明和/或示例代码。提前致谢! :)

瑞克

最佳答案

您注释掉了应该执行此操作的代码:

    // you'll want to initialize the image with some blank image as a placeholder

//cell.imageView.image = [UIImage imageNamed:@"blankthumbnail.png"];

重新启用它,你应该没问题。在您的自定义单元格中,我认为您需要将其应用于 Cell.plaatje.imageCell.aanbieder.image

关于ios - 快速滚动 UITableView 的错误图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23659754/

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