我正在尝试将序列更改为在collectionview中呈现的单元格。默认情况下,垂直布局中的集合视图单元格显示在
中1 2 3
4 5 6
7 8 9
我要做的是以下列方式更改此序列
1 2 3
6 5 4 <----- this order is reversed
7 8 9
12 11 10 <----- this order is reversed
任何人都可以告诉我如何实现这种行为。
到目前为止,我已经实现了UICollectionViewFlowLayout
并尝试操纵其方法但没有成功
注意:所有项目只能在一个部分加载
答案 0 :(得分:1)
我通过继承UICollectionViewLayout
找到了解决方案,并为每个项目计算了所需的框架。在prepareLayout
方法中。
以下代码仍有改进余地。
-(void)prepareLayout{
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
NSMutableDictionary *layoutAttr = [NSMutableDictionary dictionary];
CGFloat y=0,x=0;
for (int i = 0; i<itemCount; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGRect frame;
if (indexPath.row == 0) {
x= 0;
y=0;
frame = CGRectMake(x, y, self.itemSize.width, self.itemSize.height);
x += self.itemSize.width;
}else if (indexPath.row % 2 == 0 ) {
y += self.itemSize.height;
x -=self.itemSize.width;
frame = CGRectMake(x, y, self.itemSize.width, self.itemSize.height);
if (x <= 0) {
x += self.itemSize.width;
}else{
x -= self.itemSize.width;
}
}else{
frame = CGRectMake(x, y, self.itemSize.width, self.itemSize.height);
x += self.itemSize.width;
}
attributes.frame=frame;
attributes.zIndex = i;
layoutAttr[indexPath] = attributes;
}
layoutAttributes=layoutAttr;
}
答案 1 :(得分:0)
为了实现这一目标,您需要构建自己的集合视图布局。首先阅读文档:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCustomLayouts/CreatingCustomLayouts.html
否则,你可能很幸运,在http://cocoapods.org/找到一个 我快速搜索了一下,什么都没回来。
正如richarddas所建议的那样......
这是一个教程: http://skeuo.com/uicollectionview-custom-layout-tutorial
祝你好运!答案 2 :(得分:0)
如何实施自定义UICollectionFlowLayout
要了解如何开始使用自定义收藏视图布局,我强烈推荐此链接:http://www.objc.io/issue-3/collection-view-layouts.html
从子类化UICollectionViewLayout并覆盖layoutAttributesForItemAtIndexPath开始:
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewDataSource *dataSource = self.collectionView.dataSource;
id<CollectionViewDataSourceItem> item = [dataSource itemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
// here you can customise the frame of your item if it is on a row that is even
if (indexPath.row % 2 == 0)
{
CGFloat horizontalOffset = ???; // change to your new offset, positive or negative
CGRect rect = CGRectOffset( attributes.frame, horizontalOffset, 0);
attributes.frame = rect;
}
return attributes;
}