2014-05-22 2 views
8

UICollectionView을 사용하여 이미지가 있거나 없을 수도있는 데이터를 표시하는 프로젝트를 진행하고 있습니다.UICollectionView 셀에 이미지 추가

내가 사용하는 방법은 이미지 URL이 null이 아닌지 확인한 다음 셀에 ImageView를 추가하는 것입니다. 어떤 이미지가 없습니다 그 세포에 대한

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (question.picture != (id)[NSNull null]) { 
     //add AsyncImageView to cell 
     imageView.contentMode = UIViewContentModeScaleAspectFill; 
     imageView.clipsToBounds = YES; 
     imageView.tag = IMAGE_VIEW_TAG; 
     [cell addSubview:imageView]; 
     [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView]; 
     imageView.imageURL = [NSURL URLWithString:question.picture]; 
    } 
} 

, 셀은 다음과 같이한다 : 그러나 https://dl.dropboxusercontent.com/u/5753236/Stackoverflow/good.png

, 그들 중 일부는 여전히이 같은 원인이 그것에 자른 이미지와 이미지 뷰를 추가 할 것 : 를 https://dl.dropboxusercontent.com/u/5753236/Stackoverflow/bad.png

SDWebImage를 사용해 보았지만 여전히 문제는 해결되지 않았습니다.

다른 문제는 UICollectionView을 아래로 스크롤하면 위에 표시된 이미지로 표시된 일부 이미지가 표시되고로드가 완료된 후 올바른 이미지로 변경된다는 것이고 무엇이 원인인지 전혀 알 수 없습니다. 문제.

이 두 가지 문제를 해결할 수 있도록 도와주세요. 도움을 주시면 감사하겠습니다.

+0

링크가 잘못되었습니다 – bunkerdive

답변

13

우선, 셀에 이미지를 추가하는 방법은 매우 위험합니다. 그 이유는 셀을 다시 사용하거나 (예 : 스크롤 할 때 또는 reloadData를 사용할 때) 재사용 할 때 이미지가 제거되지 않기 때문입니다. 따라서 어디서나 사진을 볼 수 있으며 셀에 이미지가 여러 번 포함될 수도 있습니다.

  • 첫 번째 방법 (좋은 방법) : 여기 두 가지 방법이 있습니다 당신은 당신의 UICollectionViewCell를 서브 클래스, 서브 클래스에게 "이미지 뷰"속성을 제공합니다. 그런 다음 당신은 당신의 CustomCollectionViewCell.m 파일에서이 작업을 수행 :

    [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; 
    

    그것은 이미지가 제대로되어 있는지 확인합니다 :

    // Lazy loading of the imageView 
    - (UIImageView *) imageView 
    { 
        if (!_imageView) { 
         _imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds]; 
         [self.contentView addSubview:_imageView]; 
        } 
        return _imageView; 
    } 
    
    // Here we remove all the custom stuff that we added to our subclassed cell 
    -(void)prepareForReuse 
    { 
        [super prepareForReuse]; 
    
        [self.imageView removeFromSuperview]; 
        self.imageView = nil; 
    } 
    

    그런 다음 당신의 ViewController에이처럼 collectionViewCells의 새로운 클래스를 선언해야 재사용시 삭제되며 collectionView 대리인에서 셀을 설정하는 것이 더 쉽습니다.

  • 두 번째 방법 (더러운 방법), 당신이보기마다 제거 당신은 새로운 셀로드 :

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView     cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
        for (UIView *subview in [cell.contentView subviews]) { 
         [subview removeFromSuperview]; 
        } 
    
        if (question.picture != (id)[NSNull null]) { 
         //add AsyncImageView to cell 
         imageView.contentMode = UIViewContentModeScaleAspectFill; 
         imageView.clipsToBounds = YES; 
         imageView.tag = IMAGE_VIEW_TAG; 
         [cell.contentView addSubview:imageView]; 
         [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView]; 
         imageView.imageURL = [NSURL URLWithString:question.picture]; 
        } 
    } 
    

    이 방법이 훨씬 쉽습니다을하지만 난 그것을 권하고 싶지 않다 : P

이제 이것을 시도하고 버그가 어떻게 진화되었는지 알려주십시오.

+0

정말 고마워요! 제공 한 첫 번째 솔루션은 실제로 작동하며 모든 것을 해결합니다. 고맙습니다! :) – Thomas

+0

기꺼이 도와 드리겠습니다;) – Kujey

관련 문제