Ios UITableView单元格在快速滚动时显示相同的图像
我正在创建iPhone应用程序,它在表视图中显示应用程序图标和应用程序名称 我第一次下载用户文档目录中的图像,然后在字典中输入[值-图像存储文档目录路径和键是图像json URL],首先在单元格中显示图像,我检查图像是否已下载 如果已下载,则显示存储在文档目录中的本地映像,如果未下载,则下载 如果我正常滚动,单元格将显示正确的图像&如果我快速滚动,单元格将显示相同的图像,而不是不同的图像Ios UITableView单元格在快速滚动时显示相同的图像,ios,iphone,uitableview,Ios,Iphone,Uitableview,我正在创建iPhone应用程序,它在表视图中显示应用程序图标和应用程序名称 我第一次下载用户文档目录中的图像,然后在字典中输入[值-图像存储文档目录路径和键是图像json URL],首先在单元格中显示图像,我检查图像是否已下载 如果已下载,则显示存储在文档目录中的本地映像,如果未下载,则下载 如果我正常滚动,单元格将显示正确的图像&如果我快速滚动,单元格将显示相同的图像,而不是不同的图像 // code for displaying images -(void)refreshViews {
// code for displaying images
-(void)refreshViews
{
self.appLabelName.text = _applicationObject.name;
self.appLabelName.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
self.detailTextLabel.text = _applicationObject.artistName;
self.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
NSString *appIconStoredPath = [appDelgate.saveAppIconURLAndPathInFile valueForKey:_applicationObject.iconURL];
_appIcon.image = [UIImage imageWithContentsOfFile:appIconStoredPath];
if(!_appIcon.image && appDelgate.hasInternetConnection)
{
[self downloadAppIconsInDirectory];
}
}
// code for download image
-(void)downloadAppIconsInDirectory
{
NSURL *downloadURL = [NSURL URLWithString:_applicationObject.iconURL];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil delegateQueue:nil];
__weak ApplicationCell* weakSelf = self;
dispatch_async(queue, ^{
downloadTask = [session downloadTaskWithURL:downloadURL completionHandler:^(NSURL *location, NSURLResponse *respone, NSError *error)
{
NSString *iconName = [location lastPathComponent];
NSMutableString *changeIconName = [[NSMutableString alloc] init];
changeIconName = [iconName mutableCopy];
[changeIconName setString:_applicationObject.bundleId];![enter image description here][1]
NSString *appIconDirectory = [[documentsDirectoryForAppIcons absoluteString] stringByAppendingPathComponent:@"appIcons"];
destinationUrlForAppIcons = [[NSURL URLWithString:appIconDirectory] URLByAppendingPathComponent:changeIconName];
NSError *error1;
BOOL status = [appIconFileManager copyItemAtURL:location toURL:destinationUrlForAppIcons error:&error1];
if (status && !error1)
{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf refreshViews];
});
[appDelgate.saveAppIconURLAndPathInFile setValue:destinationUrlForAppIcons.path forKey:_applicationObject.iconURL];
NSString *dictSavedFilePath = [appDelgate.documentDirectoryPath stringByAppendingPathComponent:@"IconURLsAndPaths.plist"];
dispatch_async(queue, ^{
[appDelgate.saveAppIconURLAndPathInFile writeToFile:dictSavedFilePath atomically:YES];
});
}
}];
[downloadTask resume];
});
}
如图所示,代码中没有错误。这意味着您认为队列的优先级是错误的。在滚动之前必须下载图像。当你们慢慢地滚动你们的视图时,图像会有足够的时间被下载。这意味着您将代码更改为此,然后重试;)
将refreshViews方法替换为此
-(void)refreshViews
{
self.appLabelName.text = _applicationObject.name;
self.appLabelName.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
self.detailTextLabel.text = _applicationObject.artistName;
self.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
_appIcon.image = nil;
NSString *appIconStoredPath = [appDelgate.saveAppIconURLAndPathInFile valueForKey:_applicationObject.iconURL];
_appIcon.image = [UIImage imageWithContentsOfFile:appIconStoredPath];
if(!_appIcon.image && appDelgate.hasInternetConnection)
{
[self downloadAppIconsInDirectory];
}
}
它会加载上一个图像,因为tableview会重用单元格,所以imageview也会重用保存上一个图像的单元格。因此,您必须对该图像执行nil操作您需要显示UITableView委托方法的代码,以便我们可以看到您如何尝试使用该图像如果我们制作_appIcon.image=nil;它不起作用。我检查了字典内容,不同的图像URL(键)有相同的图像本地路径(值)如果我们将appIcon设为零,则不会影响任何内容。bcoz字典内容错误的图像本地路径。为了避免此问题,我使用NSURLSession委托而不是完成块。参考链接:如果我们在将内容保存到字典之前调用refreshViews方法,这并不能解决问题。有解决这个问题的其他解决方案吗?:)我认为您必须在将内容保存到字典中之后调用RefreshView,而不是之前。
-(void)refreshViews
{
self.appLabelName.text = _applicationObject.name;
self.appLabelName.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
self.detailTextLabel.text = _applicationObject.artistName;
self.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
_appIcon.image = nil;
NSString *appIconStoredPath = [appDelgate.saveAppIconURLAndPathInFile valueForKey:_applicationObject.iconURL];
_appIcon.image = [UIImage imageWithContentsOfFile:appIconStoredPath];
if(!_appIcon.image && appDelgate.hasInternetConnection)
{
[self downloadAppIconsInDirectory];
}
}