UICollectionView未显示 [英] UICollectionView Not Appearing

查看:199
本文介绍了UICollectionView未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的视图控制器中设置 UICollectionView 以编程方式扩展 UIViewController 。出于某种原因,我的集合视图根本没有出现。以下是我所拥有的。

I am trying to set up UICollectionView programatically in my view controller which extends UIViewController. For some reason, my collection view is not showing up at all. Below is what I have.

为什么不出现?我将它连接到委托和数据源,并将其作为子视图添加到 self.view 。我的代码中缺少什么?

Why is it not appearing? I am hooking it up to the delegate and data source and adding it as a subview to self.view. What's missing in my code?

在我的 .h 文件中:

In my .h file:

@interface MainViewController : UIViewController
{
    @private
    UICollectionView *_collectionView;
    NSMutableArray *_results; // data source array
}
@end

在我的 .m file:

In my .m file:

@interface MainViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *results;
@end

@implementation MainViewController

@synthesize collectionView = _collectionView;
@synthesize results = _results;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // some init stuff - nothing to do with collection view.
    }

    return self;
}

- (void)loadView
{
    self.results = [NSMutableArray array];
    UIImage *image1 = [UIImage imageNamed:@"img1.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"img2.jpg"];
    [self.results addObject:image1];
    [self.results addObject:image2];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView = collectionView;

    [self.view addSubview:self.collectionView];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.collectionView reloadData];

}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return [self.results count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image = [self.results objectAtIndex:indexPath.row];    
    return CGSizeMake(image.size.width, image.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(50, 20, 50, 20);
}


推荐答案

我在尝试运行时遇到错误你的代码,除非我将loadView方法更改为viewDidLoad - 根据你不应该直接调用loadView的文档。为了运行数据源和委托方法,我将设置委托和数据源的行移动到你设置self.collectionView = collectionView

I got errors trying to run your code unless I changed the loadView method to viewDidLoad -- according to the docs you're not supposed to directly call loadView. To get the data source and delegate methods to run, I moved the lines setting the delegate and data source to self below where you set self.collectionView = collectionView

    self.collectionView = collectionView;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

这篇关于UICollectionView未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆