5

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

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

在我的.h文件中:

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

在我的.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);
}
4

4 回答 4

3

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

    self.collectionView = collectionView;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
于 2012-09-29T23:19:33.943 回答
0

你的numberOfSectionsInCollectionView:回报0。对于具有一个部分的集合视图,您应该返回1或不实现此方法。

我也看不到你在哪里 alloc/init self.collectionView

于 2012-09-29T22:19:01.677 回答
0

我最终继承了 UICollectionViewController 而不是UIViewController将 init 方法更改为:

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout

它奏效了。

于 2012-09-29T23:34:38.317 回答
0

您只需要将您的 CollectionView 委托和数据源绑定到 StoryBoard 中的 ViewController。在此处输入图像描述

于 2015-12-15T15:12:26.167 回答