0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cell"; 
    TVcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[TVcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.titleLabel.text = [[[arrayData objectAtIndex:indexPath.row]valueForKey:@"title"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    cell.txtlabel.text = [[[arrayData objectAtIndex:indexPath.row] valueForKey:@"description"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    cell.tag = indexPath.row; 
    cell.imageView.image = nil; 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^(void) { 

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[enclosureUrlArray objectAtIndex:indexPath.row]]]; 

    UIImage* image = [[UIImage alloc] initWithData:imageData]; 
    if (image) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (cell.tag == indexPath.row) { 
       cell.IMGLabel.image = image; 
       [cell setNeedsLayout]; 
       } 
       }); 
      } 
     }); 
     return cell; 

} 

虽然我滚动,相同的图像出现在单元格,我在重复使用的单元格。我使用滚动视图委托also.im存储URL在enclosureUrlArray,我传递。tabelviewcell中的异步图像下载?

+1

尝试使用SDWebImage – Paulw11

+0

不,我不需要任何库类,sdwebimage工作正常。我想这个 – karthik

+0

然后请表现出更多的代码解决方案;显示完整的函数,其中上述从 – Paulw11

回答

2

cell.IMGLabel.image = nil放入您的cellForRowAtIndexPath下载图片之前,即低于cell.imageView.image = nil;这一行。

或者设置一些占位符图像(无可用的图像类型的图像在界面生成细胞的图像),因此,如果图像不那么下载它会显示此占位符图像,否则它会显示下载的图像。

SDWebImage有利于这种情况。它会缓存图像,这样它也会提供更好的性能。使用任何优秀的第三方库是没有错的。

0

使用SDWeb图像:

#import <SDWebImage/UIImageView+WebCache.h> 

... 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    // Here we use the new provided sd_setImageWithURL: method to load the web image 
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    cell.textLabel.text = @"My Text"; 
    return cell; 
} 
0

您还可以使用NSURLSession为好,看到这个链接

Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling

NSURL *url = [NSURL URLWithString:[enclosureUrlArray objectAtIndex:indexPath.row]]; 
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    if (data) { 
     UIImage *image = [UIImage imageWithData:data]; 
     if (image) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       MyCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath]; 
       if (updateCell) 
        updateCell.IMGLabel.image = image; 
      }); 
     } 
    } 
}]; 
[task resume]; 

工作非常适合我..

0

与的sdwebimage可以显示这样的形象帮助: -

NSString *string2 = [[businessListArray objectAtIndex:indexPath.row ]valueForKey:@"logo"]; 
    NSURL *url1 = [NSURL URLWithString: 
        [NSString stringWithFormat: 
        @"%@%@", 
        @"http://dev-demo.info.bh-in-15./upload/post/", 
        string2]]; 

    UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:301]; 

    [imageView sd_setImageWithURL:url1 
       placeholderImage:[UIImage imageNamed:@"pocket.png"] 
         completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 
          imageView.image = image; 
         }];