Как показать UICollectionView с первым разделом на левой стороне и вторым разделом на правой стороне экрана - PullRequest
0 голосов
/ 31 мая 2018

Я хочу создать UICollectionView с его первым разделом на левой стороне экрана, а второй раздел - на правой стороне экранов, например, двумя столбцами.Для каждого раздела мне нужен заголовок раздела.Есть ли способ добиться этого или любого другого пути. enter image description here

Ответы [ 2 ]

0 голосов
/ 01 июня 2018
- (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;
}
0 голосов
/ 31 мая 2018

Вам необходимо использовать UICollectionViewDelegateFlowLayout Методы

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 )
}

}

Метод minInteritemSpacingForSectionAt - интервал между вашими ячейками.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...