2013-03-30 2 views
1

UIImageView가있는 사용자 지정 셀이 있는데 이미지를 표시하지 않습니다. 기본 cell.imageView.image 속성 설정 이미지를 시도하고 잘 작동하지만 내 사용자 지정 ImageView 작동하지 않습니다.UIImageView가있는 사용자 지정 UITableViewCell이 이미지를 표시하지 않습니다.

Xib에서 내 사용자 정의 셀을로드하고 UIImageView를 게으른로드와 관련 있다고 생각합니다. 어떻게 작동하게합니까? 여기 내 코드입니다 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"MyCell"; 
    DVGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 



    cell.tag = indexPath.row; 

    if (self.loader.parsedData[indexPath.row] != nil) 
    { 
     cell.imageCustom.image = nil; 

     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
      dispatch_async(queue, ^(void) { 

       NSString *url = [self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]; 
       NSData *imageData = nil; 
       if ([self.cache objectForKey:url] != nil) 
       { 
        imageData = [self.cache objectForKey:url]; 
       } 

       else 
       { 
        imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
       [self.cache setObject:imageData forKey:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]]; 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 

        if (cell.tag == indexPath.row) { 
         UIImage *image = [[UIImage alloc] initWithData:imageData]; 
         cell.imageCustom.image = image; 
         [cell setNeedsLayout]; 
        } 
       }); 
      }); 
    } 

    return cell; 
} 
+0

동일한 코드 블록에 대한 질문을 게시하는 데 2 ​​시간의 차이가있을 경우 직접 문제를 해결하기 위해 열심히 노력하지 않습니다. –

답변

3

나는 보통이 코드 조각 ImageViews에 대한 게으른 로딩을, 그것은 도움이되기를 바랍니다 : 당신의 cellForRowAtIndexPath에서

- (void) loadImageForImageView:(UIImageView *)theImageView WithURL:(NSURL *)url {  

    NSOperationQueue *queue = [NSOperationQueue new]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:3.0]; 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *reponse, NSData *data, NSError *error) { 

     UIImage *image = [UIImage imageWithData:data]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      theImageView.image = image; 
      for (UIActivityIndicatorView *spinner in theImageView.subviews) { 
       [spinner removeFromSuperview]; 
       break; 
      } 
     }); 

    }]; 
} 

:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
[spinner setColor:[UIColor darkGrayColor]]; 
spinner.frame = CGRectMake(130 , 53, 20, 20); 
[spinner startAnimating]; 
[imageCell addSubview:spinner]; 
[self loadImageForImageView:imageCell WithURL:imageURL]; 

ImageCell로 귀하의 UIImageView입니다.

관련 문제