UITableView单元格在快速滚动时显示相同的图像

时间:2014-11-11 09:33:31

标签: ios iphone uitableview

我正在创建iPhone应用程序,显示应用程序图标&表格视图中的应用名称。

我第一次下载用户文档目录中的图像&然后在字典中输入[value - 图像存储文档目录路径&关键是图像json URL],为了在单元格中显示图像,我检查图像是否已经下载。

如果已下载,则显示存储在文档目录中的本地图像,如果没有下载它。

如果我正常滚动,则单元格显示权利图像&如果我快速滚动它,单元格会显示相同的图像而不是不同的图像。

// 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];
});
}

2 个答案:

答案 0 :(得分:0)

如图所示,代码中没有错误。这意味着你排队的优先级是错误的。滚动前必须下载图像。当您慢慢滚动视图时,图像会有足够的时间下载。这意味着您将代码更改为此并尝试;)

 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration        
 defaultSessionConfiguration];
 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil 
 delegateQueue:nil];
__weak  ApplicationCell* weakSelf = self;

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

 downloadTask = [session downloadTaskWithURL:downloadURL completionHandler:^(NSURL *location,   

      {
          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];
});
}

答案 1 :(得分:0)

用这个

替换你的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也重用了保存前一个图像的图像。所以你必须做这个图像