2013-12-13 8 views
1

이미지가로드되어 있어도 이미지가 표시되지 않는 문제가 있습니다. 나는이 여러 가지 다른 방법과 같은 결과를 시도했다 ... 이미지가 없다. 나는 스토리 보드에 지정된 크기와 색상 인 흰색 직사각형을 얻습니다. popover에서 올바른 수의 사각형을 얻습니다. 로그에는 이름과 ID가 올바르게 표시됩니다.UICollectionView 이미지가 표시되지 않습니다.

배열을 직접 호출하면 동일한 결과가 나타납니다. 흰색 직사각형.

내 대상은 iOS7입니다. Xcode 5.0.2 실행. 이미지는 SQL 데이터베이스에서옵니다. 이것은 전체 iOS7로 업데이트하고 UICollectionView에 대한 사용자 정의 그리드 레이아웃을 교체하고있는 라이브 앱입니다. CollectionViewController를 사용하여 UIButton을 통해 액세스되는 NavigationController에 포함됩니다. 보시다시피 맞춤 셀은 없습니다. 이미지는 팝 오버에 표시되며 사용자가 기본보기의 배경을 변경하도록 선택할 수 있습니다.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BckgndCell" forIndexPath:indexPath]; 

    NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row]; 
    NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:@"id"]; 
    int buttonTag = [buttonTagNumber intValue]; 
    NSString *tempImage = [tempDict objectForKey:@"fileName"]; 

    NSLog(@"Filename: %@", tempImage); 
    NSLog(@"ButtonTagNumber: %@", buttonTagNumber); 
    UIImageView *image = [[UIImageView alloc]init]; 
    image.image = [UIImage imageNamed:tempImage]; 
    NSLog(@"Image.image: %@", image.image); 

    // needed for selecting the background in didSelectItemAtIndexPath 
    _bgtag = buttonTag; 

    return cell; 
} 

실제로 cellForItemAtIndexPath 방법에서 BackgroundCell (뜨아)을 명명하고 이미지를 설정 BackgroundCell 제어기 작은 생성 방법 포함 픽스.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    BackgroundCell *cell = (BackgroundCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"BckgndCell" forIndexPath:indexPath]; 

    NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row]; 
    NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:@"id"]; 
    int buttonTag = [buttonTagNumber intValue]; 
    NSString *tempImage = [tempDict objectForKey:@"fileName"]; 

    [cell setImage:[UIImage imageNamed:tempImage]]; 

    // needed for selecting the background in didSelectItemAtIndexPath 
    _bgtag = buttonTag; 

    return cell; 
} 

주 셀 코드;

- (id)initWithFrame:(CGRect)frame 
{ 
    _backgroundImage = [[UIImageView alloc] initWithFrame:self.contentView.bounds]; 

    [self.contentView addSubview:_backgroundImage]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

-(void)setImage:(UIImage *)image 
{ 
    _backgroundImage.image = image; 
} 
+0

나는 지금 보여주는 이미지가 있습니다 ... 다른 작업을 위해 오랜 휴식을 취했습니다. 중요한 것은 셀 컨트롤러와 몇 가지 다른 것들을 호출하는 것입니다. 내 수정 프로그램이 위에 추가되었습니다. –

답변

4

문제는 이미지가 셀의보기 계층 구조에 설정되어 있지 않다는 것입니다. 그 initWithFrame에있는 UIImageView를

@interface CollectionViewCellSubclass : UICollectionViewCell 
@property (nonatomic, strong) UIImageView *imageView; 
@end 

... 그리고 초기화하기 : 그, 서브 클래스 UICollectionViewCell을하고 서브 클래스에 imageView 속성을 만들려면

_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds]; 
[self.contentView addSubview:_imageView]; 

는 셀의 사용자 정의 클래스 등의 서브 클래스를 설정

CollectionViewCellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BckgndCell" forIndexPath:indexPath]; 

//... 

cell.imageView.image = [UIImage imageNamed:tempImage]; 

호 : 다음은 스토리 보드와 셀의 하위 뷰 이미지에 이미지를 설정 위에 collectionView:cellForItemAtIndexPath 방법로드 이것은 도움이됩니다.

+0

이것은 가깝지만 시가가 없습니다. 주요 질문 영역의 수정 사항을 참조하십시오. –

+2

@MarcWatson 요점은 당신이'UICollectionViewCell'을 서브 클래스 화하고'imageView'를 contentView에 추가했다는 것입니다. 'initWithFrame'에서 초기화되어야합니다. 그래서 위의 편집을했습니다. 대답의 요지는 정확하지만 올바른 경로로 설정하면 아마 받아 들여질 가치가있을 것입니다. –

1

dequeueReusableCellWithReuseIdentifier이 나를 위해 일한 후 cell.contentView.frame = [cell bounds]을 추가하십시오.

xib를 통해 맞춤 (200,200) UICollectionViewCell을 추가하고 코드 어딘가에 프레임을 예를 들어 변경한다고 가정 해 보겠습니다. (120,120). 콘텐츠 뷰의 프레임을 적절히 설정해야합니다. 그렇지 않으면 imageView (콘텐츠 뷰에 ​​있음)의 이미지가 제대로 표시되지 않습니다.

+0

답변을 편집 할 때 그 정보를 게시 했어야합니다. 나는 당신의 대답을 편집했습니다. – Palec

관련 문제