gpt4 book ai didi

ios - 使用自定义布局的 UICollectionView 项目顺序

转载 作者:行者123 更新时间:2023-11-30 12:35:43 25 4
gpt4 key购买 nike

我正在使用 swift 实现 Collection View (垂直滚动),并且项目的顺序需要如下所示。

<小时/>

项目1项目5

项目2项目6

项目3项目7

项目4项目8

但是,默认的flowlayout排序是这样的。

<小时/>

项目1项目2

项目3项目4

项目5项目6

项目7项目8

谁能告诉我如何使用自定义布局实现我需要的排序?提前致谢。

最佳答案

您需要创建 UICollectionViewFlowLayout 的子类。从那里您可以使用 layoutAttributesForItemAtIndexPath: 计算每个单元格的框架。

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {

UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

int half = NUM_ITEMS + 1 / 2;
int row = indexPath.row % half;
int col = indexPath.row <= half ? 0 : 1;
attributes.frame = CGRectMake(CELL_WIDTH * col, CELL_HEIGHT * row, CELL_WIDTH, CELL_HEIGHT);

return attributes;
}

在您的示例中,索引 3 处的项目 (Item4) 将具有框架 0、CELL_HEIGHT * 3、CELL_WIDTH、CELL_HEIGHT(左下角)和索引处的项目4 (Item5) 将具有框架 CELL_WIDTH, 0, CELL_WIDTH, CELL_HEIGHT(右上角)

关于ios - 使用自定义布局的 UICollectionView 项目顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42891198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com