Reorder cells of UICollectionView

前端 未结 4 1298
半阙折子戏
半阙折子戏 2020-12-12 19:07

Consider a UICollectionView with flow layout and horizontal direction. By default, cells are ordered from top to bottom, left to right. Like this:



        
4条回答
  •  無奈伤痛
    2020-12-12 19:53

    This is my solution, I did not test with animations but I think it will be work well with a little changes, hope it helpful

    CELLS_PER_ROW = 4;
    CELLS_PER_COLUMN = 5;
    CELLS_PER_PAGE = CELLS_PER_ROW * CELLS_PER_COLUMN;
    
    UICollectionViewFlowLayout* flowLayout = (UICollectionViewFlowLayout*)collectionView.collectionViewLayout;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = new CGSize (size.width / CELLS_PER_ROW - delta, size.height / CELLS_PER_COLUMN - delta);
    flowLayout.minimumInteritemSpacing = ..;
    flowLayout.minimumLineSpacing = ..;
    ..
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                      cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        NSInteger index = indexPath.row;
        NSInteger page = index / CELLS_PER_PAGE;
        NSInteger indexInPage = index - page * CELLS_PER_PAGE;
        NSInteger row = indexInPage % CELLS_PER_COLUMN;
        NSInteger column = indexInPage / CELLS_PER_COLUMN;
    
        NSInteger dataIndex = row * CELLS_PER_ROW + column + page * CELLS_PER_PAGE;
    
        id cellData = _data[dataIndex];
        ...
    }
    

提交回复
热议问题