簡體   English   中英

如何在UIView中設置UICollectionView

[英]How to set UICollectionView inside UIView

我想在UIView類中添加UICollectionViewControllers

我無法添加控件。

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

將UICollectionView控件添加到您的xib並設置它的出口,並將其委托方法UICollectionViewDataSource和UICollectionViewDelegate添加到.h文件。

將以下代碼添加到.m文件中

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

如果您正在使用自定義單元格,並且您想要重新加載集合視圖而不是添加以下代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM