2013-04-04 79 views
0

在异步映像出现问题时不知道为什么会出现这个奇怪的问题。我正在应用异步图像加载技术,但图像在滚动时加载后不会冻结,而是再次加载。在Uitableview单元格中一次又一次地加载异步映像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell=[self getCellContentView:CellIdentifier]; 
    } 
    [cell setBackgroundColor:[UIColor redColor]]; 
    NSLog(@"%i",[discussionArray count]); 
    NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row]; 
    UILabel *textLabel1 = (UILabel *)[cell viewWithTag:1]; 
    UILabel *textLabel2 = (UILabel *)[cell viewWithTag:2]; 
    UILabel *textLabel3 = (UILabel *)[cell viewWithTag:3]; 
    UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4]; 
    UIImageView *new_oldimage = (UIImageView *)[cell viewWithTag:5]; 
    UIImageView *avatarimage_cover = (UIImageView *)[cell viewWithTag:6]; 

    textLabel1.text =[dict objectForKey:@"user"]; 
    textLabel2.text =[dict objectForKey:@"date"]; 
    textLabel3.text =[dict objectForKey:@"text"]; 

    NSString *photoStrn=[dict objectForKey:@"photo"]; 
    avatarimage.image = nil; 
    dispatch_async(dispatch_get_global_queue(0,0), ^{ 
     NSString *u=[NSString stringWithFormat:@"http://%@",photoStrn]; 
     NSURL *imageURL=[NSURL URLWithString:u]; 
     NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      UIImage *dpImage = [UIImage imageWithData:imageData]; 
      if (dpImage==nil) 
      { 
       dpImage = [UIImage imageNamed:@"profileImage.png"]; 
      } 


      [UIView animateWithDuration:5.0 animations:^{ 
       avatarimage.alpha = 1.0; 
      }]; 
      avatarimage.image = dpImage; 

     }); 

    }); 

    avatarimage_cover.image = [UIImage imageNamed:@"avtrcover.png"]; 

    NSString *new=[dict objectForKey:@"new"]; 
    if ([new isEqualToString:@"0"]) 
    { 
     new_oldimage.image = [UIImage imageNamed:@"old_message.png"]; 
    } 
    else 
    { 
     new_oldimage.image = [UIImage imageNamed:@"new_message.png"]; 
    } 

    textLabel3.numberOfLines = 0; 
    NSString *detailText = [[discussionArray objectAtIndex:indexPath.row] objectForKey:@"text"]; 
    CGSize textSize = [detailText sizeWithFont:[UIFont boldSystemFontOfSize:13] constrainedToSize:CGSizeMake(240, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 
    textLabel3.frame = CGRectMake(70, 53, 240, textSize.height+detailTextoffset); 
    return cell; 


} 

回答

0

您在设置​​时每次配置单元时均为零。无论什么,您都可以调用加载代码。因此,重新加载图像并不奇怪。

相反,您应该维护一个可变数组的下载图像。这可能只是文件文件夹中某处图像的文件URL。如果图像存在使用它,如果没有下载它。

if (imageArray[indexPath.row]) { 
    // use the image to populate the image view 
} 
else { 
    // fetch async from server 
} 
+0

我实现了,但现在的图像彼此是否有他们交换有状态的两个图像的变化为例。 – 2013-04-05 11:15:18

0

这将彻底解决您的问题。请点击以下链接:

Image on top of Cell

+0

绑这个,但不为我工作:( – 2013-04-05 15:39:07

+0

它不可能。你可以告诉我你的代码 – 2013-04-06 06:01:14

相关问题