简体   繁体   中英

How to set UICollectionView inside UIView

I am trying to add UICollectionViewControllers inside UIView class.

I cant add the controls.

CGRect viewframes=CGRectMake(0,400,self.view.bounds.size.width, 
self.view.bounds.size.height/2);

self.button=[[view2 alloc]initWithFrame:viewframes];   
self.button.backgroundColor=[UIColor grayColor]; 
[self.view addSubview:self.button]; 

Add UICollectionView control to your xib and set outlet of that and also add it's delegate methods UICollectionViewDataSource and UICollectionViewDelegate to .h file .

Add below code to your .m file

 #pragma mark Collection View Methods
 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
     return UIEdgeInsetsMake(10, 10, 10, 10);
 }

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 {
     return [urlArray count];
 }

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
 {
     return CGSizeMake(140, 140);
 }

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
  GalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
     cell.tag=indexPath.row;
     return cell;
 }

If you are using custom cell and you want to reload Collection view than add below code

[YourCollectionview registerNib:[UINib nibWithNibName:@"GalleryCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
[YourCollectionview reloadData];

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