18

我試圖重新實現不定式滾動UICollectionView看到here。那名失蹤我的事情:而不是UICollectionView顯示黑屏

ViewController.h

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate> 

@end 

DataCell.h

@interface DataCell : UICollectionViewCell 
@property (nonatomic, strong) UILabel *label; 
@end 

DataCell.m

#import "DataCell.h" 

@implementation DataCell 

-(instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if(self){ 
     self.label = [[UILabel alloc] initWithFrame:self.bounds]; 
     self.autoresizesSubviews = YES; 
     self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | 
             UIViewAutoresizingFlexibleHeight); 
     self.label.textAlignment = NSTextAlignmentCenter; 
     self.label.adjustsFontSizeToFitWidth = YES; 

     [self addSubview:self.label]; 
    } 

    return self; 
} 

@end 

CustomCollectionView.h:

@interface CustomCollectionView : UICollectionView 

@end 

對於我用故事板整個項目和正常UIViewController。在這個視圖控制器上,我在Interface Builder中添加了一個UICollectionView。我將集合視圖的出口與視圖控制器連接起來,並將數據源和委託方法再次設置到我的視圖控制器。我還在Interface Builder中設置了UICollectionViewCell的自定義類和重用標識符。

所以一切都應該工作,但我只會得到一個黑屏。我錯過了什麼?您可以下載整個項目here

回答

23

你正確配置的CollectionView,只是你忘記了:)標籤的顏色

[self.label setTextColor:[UIColor whiteColor]]; 

enter image description here

希望它能幫助!

4

我有同樣的問題。黑色屏幕似乎是集合視圖無法顯示數據的指標。嘗試更改集合視圖的背景顏色,如果顯示的顏色已更改,您的集合視圖正在工作。然後將一些imageview添加到帶有標記的集合視圖(例如,爲圖像視圖的標記值賦予值100),並使用cellforItemAtIndexPath將圖像設置爲圖像視圖。 (您可以通過自定義單元做到這一點。但現在,獲取集合視圖工作,標籤分配爲ImageView的西服更好)

UIImageView * ImageView = (UIImageView *)[cell viewWithTag:100]; 
ImageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]]; 
3

您需要手動設置集合視圖的背景顏色在故事板中。

默認情況下它是黑色的(雖然沒有顯示在故事板編輯)

enter image description here

+0

如果您希望背景爲cl耳朵,你必須使用'collectionView.backgroundColor = [UIColor clearColor];''和'collectionView。backgroundView = [[UIView alloc] initWithFrame:CGRectZero];' – Multinerd 2016-12-01 21:11:59

1

在斯威夫特,

self.label.textColor = UIColor.whiteColor() 
0
[self.collectionView registerNib:[UINib nibWithNibName:@"ProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ProductCollectionViewCell"]; 

self.collectionView.backgroundColor = [UIColor clearColor]; 
1

它發生在我身上,無論是的CollectionView和集合視圖單元具有透明背景