【问题标题】:why uicollectionview not showing anything为什么 uicollectionview 没有显示任何内容
【发布时间】:2017-08-24 11:23:10
【问题描述】:

尝试如下使用UICollectionView

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>

@property(strong, nonatomic) UICollectionView *collectionView;
@end

还有.m

#import "ViewController.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation ViewController
@synthesize collectionView;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
    collectionView=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    [collectionView setDataSource:self];
    [collectionView setDelegate:self];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [collectionView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:collectionView];
}



-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 15;
}

-(UICollectionViewCell *) collectionView:(UICollectionView *)mycollectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[mycollectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    cell.backgroundColor=[UIColor redColor];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50, 50);
}
@end

我打算在红色背景上显示绿色矩形列表,但只有红色背景显示,我错过了什么?

【问题讨论】:

  • 确认 UICollectionViewDelegateFlowLoayout 协议。
  • 提供屏幕截图。您所期待的以及即将到来的。
  • UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

标签: ios objective-c uicollectionview


【解决方案1】:

在初始化布局时使用“UICollectionViewFlowLayout”代替“UICollectionViewLayout”。

UICollectionViewLayout *layout = [[UICollectionViewFlowLayout alloc] init];

link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多