2013-04-04 74 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

相關問題