我正在尝试重新实现看到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。
答案 0 :(得分:24)
您正在正确配置CollectionView,只是忘记了标签的颜色:)
[self.label setTextColor:[UIColor whiteColor]];
希望它有所帮助!
答案 1 :(得分:5)
答案 2 :(得分:4)
我有同样的问题。黑屏似乎是无法显示集合视图的数据的指示器。尝试更改集合视图的背景颜色,如果要显示更改的颜色,则集合视图正在运行。然后使用标记将一些imageview添加到集合视图中(例如,使用图像视图的标记值给出值100),并使用cellforItemAtIndexPath将图像设置为图像视图。 (您可以使用自定义单元格执行此操作。但是现在,要使集合视图正常工作,带有imageview标记的分配更适合)
UIImageView * ImageView = (UIImageView *)[cell viewWithTag:100];
ImageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
答案 3 :(得分:2)
我发现collectionView和集合视图单元都有透明背景
答案 4 :(得分:1)
在斯威夫特,
self.label.textColor = UIColor.whiteColor()
答案 5 :(得分:1)
[self.collectionView registerNib:[UINib nibWithNibName:@"ProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ProductCollectionViewCell"];
self.collectionView.backgroundColor = [UIColor clearColor];