首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionView不显示内部的细胞

UICollectionView不显示内部的细胞
EN

Stack Overflow用户
提问于 2018-10-29 12:02:46
回答 1查看 102关注 0票数 0

我正在创建一个UIView,里面有UICollectionView,但问题是我的UICollectionView没有显示单元格。它只是显示一个黑色的背景,像下面的图片。我必须用视图调试器来检查它,但是集合视图中没有任何内容,尝试在numberOfItemsInSection & cellForItemAtIndexPath上放置一个断点,并注意到它没有执行。我必须看看这个论坛的结果,但没有任何答案能解决我的问题。

下面是我的init集合视图的代码,初始化UICollectionView时是否出错了?

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-29 13:37:59

在初始化dataSource之前,您正在设置委托和collectionView。

您应该更新代码如下所示。

代码语言:javascript
复制
-(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];
}

试着分享你的成果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53045105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档