更改uicollectionviewcell的顺序

时间:2015-03-20 10:53:26

标签: ios uicollectionview uicollectionviewlayout

我正在尝试将序列更改为在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并尝试操纵其方法但没有成功

注意:所有项目只能在一个部分加载

3 个答案:

答案 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

您还应该查看WWDC视频: http://devstreaming.apple.com/videos/wwdc/2014/232xxz8gxpbstio/232/232_hd_advanced_user_interfaces_with_collection_views.mov?dl=1

祝你好运!

答案 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;
}