如何使用storyboard将CollectionView添加到ViewController?

时间:2014-08-18 10:40:49

标签: ios objective-c uiviewcontroller storyboard uicollectionview

我已将collectionView移至我的ViewController,创建了IBOutlet,与dataSourcedelegate建立了连接,添加了协议:UICollectionViewDataSourceUICollectionViewDelegate,{{ 1}},还添加了方法:

UICollectionViewDelegateFlowLayout

我还创建了自定义单元格并将其添加到- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section { return 5; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (AdImageCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { AdImageCell *cell = (AdImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"AdImageCellIdentifier" forIndexPath:indexPath]; cell.imageView1.image = [UIImage imageNamed:@"3.jpg"]; return cell; }

viewDidLoad

但我的问题是 [self.collectionView registerNib:[UINib nibWithNibName:@"AdImageCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"AdImageCellIdentifier"]; numberOfItemsInSection仅被调用,而不是numberOfSectionsInCollectionView。为什么呢?

1 个答案:

答案 0 :(得分:0)

如果您从collectionView registerClass:forCellWithReuseIdentifier方法移除viewDidLoad,有时它会起作用。

collectionView:cellForItemAtIndexPath: never gets called

希望它会帮助你..