简体   繁体   中英

How to add CollectionView to ViewController with storyboard?

I have moved collectionView to my ViewController , created IBOutlet, established connection with dataSource and delegate , added protocols: UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewDelegateFlowLayout , and also added methods:

- (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;
}

I also created custom cell and add this to viewDidLoad :

   [self.collectionView registerNib:[UINib nibWithNibName:@"AdImageCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"AdImageCellIdentifier"];

But my problem here is that numberOfItemsInSection and numberOfSectionsInCollectionView is only called, but not cellForItemAtIndexPath . Why?

Sometimes it will work if you remove the collectionView registerClass:forCellWithReuseIdentifier from the viewDidLoad method.

collectionView:cellForItemAtIndexPath: never gets called

Hope it will help you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM