Ios UICollectionView不显示内部单元格
我正在创建一个内部带有UICollectionView的UIView,但问题是我的UICollectionView没有显示单元格。它只是显示一个黑色背景,如下图所示。我必须用view Debugging检查它,但在collection视图中没有任何内容,尝试在numberOfItemsInSection&cellForItemAtIndexPath处放置一个断点,注意它没有执行。我必须看看这个论坛的结果,但没有答案可以解决我的问题 这是我初始化集合视图的代码,我在初始化UICollectionView时出错了吗Ios UICollectionView不显示内部单元格,ios,uicollectionview,Ios,Uicollectionview,我正在创建一个内部带有UICollectionView的UIView,但问题是我的UICollectionView没有显示单元格。它只是显示一个黑色背景,如下图所示。我必须用view Debugging检查它,但在collection视图中没有任何内容,尝试在numberOfItemsInSection&cellForItemAtIndexPath处放置一个断点,注意它没有执行。我必须看看这个论坛的结果,但没有答案可以解决我的问题 这是我初始化集合视图的代码,我在初始化UICollectionV
#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;
}
在初始化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];
}
尝试并分享您的结果。为什么要在collectionView中同时注册class和nib?另外,请尝试将collectionView的backgroundColor设置为UIColor.white。@PGDev我删除了register类,因为我对单元格使用了nib文件,所以我忘记删除该行。即使我将集合视图的背景设置为任何颜色。没用了,谢谢。成功了!我真的忘了在初始化collectionview之后设置委托。再次感谢。