我正在创建一个带有UICollectionView的UIView,但是问题是我的UICollectionView没有显示单元格。只是显示黑色背景,如下图所示。我必须使用视图调试来检查它,但是在集合视图中什么也没有检查,请尝试在numberOfItemsInSection
和cellForItemAtIndexPath
处放置一个断点,并注意它没有执行。我必须看一下这个论坛的结果,但是没有答案可以解决我的问题。
这是我用于初始化集合视图的代码,初始化UICollectionView时是否出错?
#define IS_IPAD_IDIOM (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
@interface HDSGridPopupMenu ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *gridMenuCollection;
@end
@implementation HDSGridPopupMenu
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self commonInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self commonInit];
}
return self;
}
-(void)commonInit {
self.clipsToBounds = YES;
self.layer.borderColor = UIColor.greenColor.CGColor;
self.layer.borderWidth = 2;
self.layer.cornerRadius = 5;
self.backgroundColor = UIColor.whiteColor;
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake([self popupSize].size.width/2-75, 5, 150, 30)];
logo.backgroundColor = [UIColor redColor];
[self addSubview:logo];
[self.gridMenuCollection registerClass:[HDSGridPopupMenuCell class] forCellWithReuseIdentifier:@"defaultCell"];
[self.gridMenuCollection registerNib:[UINib nibWithNibName:@"HDSGridPopupMenuCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"defaultCell"];
self.gridMenuCollection.backgroundColor = [UIColor clearColor];
self.gridMenuCollection.delegate = self;
self.gridMenuCollection.dataSource = self;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.gridMenuCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, logo.frame.size.height + logo.frame.origin.y + 5, [self popupSize].size.width, [self popupSize].size.height - logo.frame.size.height - logo.frame.origin.y) collectionViewLayout:flowLayout];
// [flowLayout setItemSize:CGSizeMake(100, 100)];
[self addSubview:self.gridMenuCollection];
}
-(CGRect)popupSize {
CGFloat spreadWidth = IS_IPAD_IDIOM? 390: (IS_IPHONE_5_OR_LESS?255:390);
CGRect screenFrame = [UIScreen mainScreen].bounds;
CGRect frame = CGRectMake((CGRectGetWidth(screenFrame) - spreadWidth) / 2,
(CGRectGetHeight(screenFrame) - spreadWidth) / 2,
spreadWidth - 20, spreadWidth);
return frame;
}
#pragma mark - CollectionView Setup
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 9;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(collectionView.frame.size.width / 3, collectionView.frame.size.width / 3);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HDSGridPopupMenuCell *defaultCell = (HDSGridPopupMenuCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"defaultCell" forIndexPath:indexPath];
defaultCell.contentView.backgroundColor = [UIColor redColor];
return defaultCell;
}
答案 0 :(得分:1)
您要在初始化collectionView之前设置委托和数据源。
您应该将代码更新为如下所示。
-(void)commonInit {
self.clipsToBounds = YES;
self.layer.borderColor = UIColor.greenColor.CGColor;
self.layer.borderWidth = 2;
self.layer.cornerRadius = 5;
self.backgroundColor = UIColor.whiteColor;
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake([self popupSize].size.width/2-75, 5, 150, 30)];
logo.backgroundColor = [UIColor redColor];
[self addSubview:logo];
//CREATE FLOWLAYOUT AND INITIALISE COLLECTIONVIEW
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.gridMenuCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, logo.frame.size.height + logo.frame.origin.y + 5, [self popupSize].size.width, [self popupSize].size.height - logo.frame.size.height - logo.frame.origin.y) collectionViewLayout:flowLayout];
// [flowLayout setItemSize:CGSizeMake(100, 100)];
[self.gridMenuCollection registerClass:[HDSGridPopupMenuCell class] forCellWithReuseIdentifier:@"defaultCell"];
[self.gridMenuCollection registerNib:[UINib nibWithNibName:@"HDSGridPopupMenuCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"defaultCell"];
self.gridMenuCollection.backgroundColor = [UIColor clearColor];
self.gridMenuCollection.delegate = self;
self.gridMenuCollection.dataSource = self;
[self addSubview:self.gridMenuCollection];
}
尝试并分享您的结果。