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

셀을 구성 할 때마다 avatarimage.image을 nil로 설정하고 있습니다. 무엇이든 관계없이 로딩 코드를 호출합니다. 따라서 이미지를 다시로드하는 것은 놀라운 일이 아닙니다.

대신 다운로드 한 이미지가 변경 가능한 배열을 유지해야합니다. 이것은 문서 폴더의 어딘가에있는 이미지 파일 URL 일 수 있습니다. 이미지가 있으면 그것을 사용하고, 다운로드하지 않는다면 사용하십시오.

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

을 나는 그것을 구현하지만,이 상태를 교환하는 두 개의 이미지가있는 경우 이제 이미지는 예를 들어 서로 변화하고있다. –

0

이렇게하면 문제가 완전히 해결됩니다. 아래 링크를 클릭 :

Image on top of Cell

+0

이 묶여 있지만 나를 위해 작동하지 않습니다 : ( –

+0

그것은 할 수 없습니다. 귀하의 코드를 보여줄 수 있습니까? –

관련 문제