【问题标题】:How to show UICollectionView with first section on left side and second section right side of the screen如何在屏幕左侧显示第一部分和右侧第二部分的 UICollectionView
【发布时间】:2018-05-31 13:19:05
【问题描述】:

我想创建一个 UICollectionView,其第一部分位于屏幕左侧,第二部分位于屏幕右侧,如两列。对于每个部分,我需要一个部分标题。 有没有办法实现这个或任何替代方式。

【问题讨论】:

  • 我认为,如果您将项目宽度设置为 collectionViewwidth/2,您可以将 section1 的元素成对放入 indexpath.row,并将第 2 部分的元素放入赔率 indexPath.row
  • 您可以使用自定义布局。
  • @ReinierMelian 实际上我需要这样 - 我已经用图片更新了问题。

标签: ios objective-c iphone uicollectionview


【解决方案1】:

你需要使用UICollectionViewDelegateFlowLayoutMethods

extension HomeVC: UICollectionViewDelegateFlowLayout{

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 16
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: (collectionView.bounds.size.width)/2 - 8  , height: (collectionView.bounds.size.width)/2 )
}

}

minimumInteritemSpacingForSectionAt 方法是单元格之间的间距。

【讨论】:

    【解决方案2】:
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.data = @[@[[UIColor blackColor],[UIColor blackColor],[UIColor blackColor]],        //section 1
                      @[[UIColor yellowColor],[UIColor yellowColor],[UIColor yellowColor]]];    //section 2
    
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self.collectionView setContentInset:UIEdgeInsetsMake(8, 8, 8, 8)];
        [self setupCollectionViewCellSize];
    }
    
    - (void)setupCollectionViewCellSize {
        int numOfCell = 2, paddingBetweenCell = 8;
        float width = [UIScreen mainScreen].bounds.size.width - (self.collectionView.contentInset.left + self.collectionView.contentInset.right);   //minus left and right Inset
        float cellWidth = (width - (numOfCell * paddingBetweenCell)) / numOfCell;
        self.cellSize = CGSizeMake(cellWidth, cellWidth);
    }
    
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return self.cellSize;
    }
    
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 1;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return [[self.data objectAtIndex:0] count] + [[self.data objectAtIndex:1] count];
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        if(indexPath.item%2==0) {
            cell.backgroundColor = [[self.data objectAtIndex:0] objectAtIndex: (indexPath.item%[[self.data objectAtIndex:0] count])];
        } else {
            cell.backgroundColor = [[self.data objectAtIndex:1] objectAtIndex: (indexPath.item%[[self.data objectAtIndex:1] count])];
        }
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 2020-12-14
      • 1970-01-01
      • 2019-11-10
      • 2012-06-06
      相关资源
      最近更新 更多