UICollectionView - 水平排列单元格 [英] UICollectionView - order cells horizontally

查看:36
本文介绍了UICollectionView - 水平排列单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我的问题是我的 uicollectionview 的单元格是从上到下排列的,而不是从左到右排列的.

So basically my problem is that my uicollectionview's cells are ordered from top to bottom, rather than from left to right.

This is what it looks like -
[1][4][7]
[2][5][8]
[3][6][9]

This is what i want -
[1][2][3]
[4][5][6]
[7][8][9]

另一个问题是当我水平滚动而不是将页面移动整整 320 点时.它仅移动到足以容纳视图中的下一个单元格.

Another problem is when i scroll horizontally instead of moving the page a full 320points. It only moves enough to fit the next cell in the view.

this is what it looks like -
    [2][3][10]
    [5][6][  ]
    [8][9][  ]

this is what i want -
    [10][ ][ ]
    [  ][ ][ ]
    [  ][ ][ ]

我希望水平渲染它会修复它.我还是 ios 的新手,所以请告诉我怎么做,我搜索的所有 tuts 都不是我的确切问题或者已经过时了.

I was hoping rendering it horizontally would fix it though. I'm still new to ios so please show me how to do it, all the tuts i searched are not for my exact problem or is outdated.

推荐答案

这是我的解决方案,希望对您有所帮助:

This is my solution, 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];
    ...
}

这篇关于UICollectionView - 水平排列单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆