2013-12-13 49 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

問題是圖像沒有在單元格的視圖層次結構中設置。要做到這一點,子類UICollectionViewCell並在子類中創建一個imageView屬性:

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

...和init的的UIImageView在其initWithFrame

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

設置此子類的自定義類細胞在情節串連圖板,然後加載它在collectionView:cellForItemAtIndexPath方法上面的圖像設置到該單元的圖像子視圖:

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

//... 

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

何這有助於。

+0

這很近,但沒有雪茄。查看主要問題區域中的修復。 –

+2

@MarcWatson關鍵在於你已經將子類「UICollectionViewCell」添加到了contentView中。它應該已經在'initWithFrame'中初始化了,所以我在上面做了編輯。答案的要點雖然準確,但可能會讓你走上正確的道路,所以也許值得接受。 –

1

添加cell.contentView.frame = [cell bounds]dequeueReusableCellWithReuseIdentifier後爲我工作。

假設您通過xib添加自定義(200,200)UICollectionViewCell,並將其框架中的某處更改爲例如「 (120,120)。您必須相應地設置其內容視圖的框架,否則其imageView上的圖像(在其內容視圖中)將不會正確顯示。

+0

你應該發佈該信息作爲你的答案的編輯。我在你的回答中編輯了你的評論。 – Palec