xcode UICollectionView 没有出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12656910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView Not Appearing
提问by darksky
I am trying to set up UICollectionViewprogramaticallyin my view controller which extends UIViewController. For some reason, my collection view is not showing up at all. Below is what I have.
我试图在我的视图控制器中以UICollectionView编程方式设置扩展UIViewController. 出于某种原因,我的集合视图根本没有显示。下面是我所拥有的。
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?
为什么不出现?我将它连接到委托和数据源,并将其作为子视图添加到self.view. 我的代码中缺少什么?
In my .hfile:
在我的.h文件中:
@interface MainViewController : UIViewController
{
@private
UICollectionView *_collectionView;
NSMutableArray *_results; // data source array
}
@end
In my .mfile:
在我的.m文件中:
@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);
}
采纳答案by rdelmar
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
我在尝试运行您的代码时遇到错误,除非我将 loadView 方法更改为 viewDidLoad——根据文档,您不应该直接调用 loadView。为了运行数据源和委托方法,我将设置委托和数据源的行移动到 self 下面设置 self.collectionView = collectionView
self.collectionView = collectionView;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
回答by darksky
I ended up subclassing UICollectionViewController instead of UIViewControllerand changing the init method to:
我最终继承了 UICollectionViewController 而不是UIViewController并将 init 方法更改为:
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
and it worked.
它奏效了。
回答by Pek Mario
You only had to bind your CollectionView delegate, and dataSource to ViewController in the StoryBoard.enter image description here
您只需将 CollectionView 委托和 dataSource 绑定到 StoryBoard 中的 ViewController。在此处输入图片说明
回答by Martin R
Your numberOfSectionsInCollectionView:returns 0. For a collection view with one section, you should either return 1or just not implement this method.
你的numberOfSectionsInCollectionView:回报0。对于包含一个部分的集合视图,您应该返回1或不实现此方法。
Also I cannot see where you alloc/init self.collectionView.
我也看不到你在哪里 alloc/init self.collectionView。

