Reputation: 537
I'm creating an UIView with UICollectionView inside but the problem is my UICollectionView doesn't show the cells. It's just display a black background like the image below. I have to check it with view debuging but nothing in the collection view, try to put an breakpoint at numberOfItemsInSection
& cellForItemAtIndexPath
and notice it's doesn't execute. I have to take a look at the results in this forum but no answer can resolve my problem.
Here is my code for init the collection view, did I wrong when initialize the 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;
}
Upvotes: 0
Views: 104
Reputation: 2916
You are setting the delegate and dataSource before initialising the collectionView.
You should update your code to look as follows.
-(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];
}
Try and share your results.
Upvotes: 1