UICollectionView Set number of columns

后端 未结 17 1171
执笔经年
执笔经年 2020-11-28 17:28

I just started learning about UICollectionViews. I\'m wondering if anyone knows how to specify the number of columns in a collectionview. The default is set to 3 (iPhone/por

17条回答
  •  Happy的楠姐
    2020-11-28 18:13

    Try This,It's working perfect for me,

    Follow below steps:

    1. Define values in .h file.

         #define kNoOfColumsForCollection 3
         #define kNoOfRowsForCollection 4
         #define kcellSpace 5
         #define kCollectionViewCellWidth (self.view.frame.size.width - kcellSpace*kNoOfColumsForCollection)/kNoOfColumsForCollection
         #define kCollectionViewCellHieght (self.view.frame.size.height-40- kcellSpace*kNoOfRowsForCollection)/kNoOfRowsForCollection
    

    OR

      #define kNoOfColumsForCollection 3
      #define kCollectionViewCellWidthHieght (self.view.frame.size.width - 6*kNoOfColumsForCollection)/kNoOfColumsForCollection
    

    2.Add Code in collection View Layout data source methods as below,

     #pragma mark Collection View Layout data source methods
    // collection view with autolayout
    
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
    {
    return 4;
    }
    
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
     {
     return 1;
    }
    
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
    {
    return UIEdgeInsetsMake(4, 4, 4, 4);
    }
    - (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
       sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
     return 
     CGSizeMake(kCollectionViewCellWidth,kCollectionViewCellHieght);
     // CGSizeMake (kCollectionViewCellWidthHieght,kCollectionViewCellWidthHieght);
    }
    

    Hope this will be help for some one.

提交回复
热议问题