【问题标题】:How to add CollectionView to ViewController with storyboard?如何使用情节提要将 CollectionView 添加到 ViewController?
【发布时间】:2014-08-18 10:40:48
【问题描述】:

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

- (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"];

但我的问题是 numberOfItemsInSectionnumberOfSectionsInCollectionView 只被调用,而不是 cellForItemAtIndexPath。为什么?

【问题讨论】:

    标签: ios objective-c uiviewcontroller storyboard uicollectionview


    【解决方案1】:

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

    collectionView:cellForItemAtIndexPath: never gets called

    希望对你有所帮助..

    【讨论】:

    • @dfasfasdfdfdfasdfasf 确保您已在情节提要的集合视图中添加原型单元格,并将该单元格的类设置为 AdImageCell
    • vitalyster,如果我从 .xib 加载单元格,是否需要将此添加到 collectionView?
    猜你喜欢
    • 2020-06-02
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多