更改 uicollectionviewcell 的顺序
Change the sequence of uicollectionviewcell
我正在尝试将序列更改为在 collectionview 中呈现的单元格。默认情况下,在垂直布局中,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
并试图操纵它的方法但是没有成功
注意:所有项目只能在一个部分加载
为了实现这一点,您将需要构建自己的集合视图布局。首先阅读此处的文档:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCustomLayouts/CreatingCustomLayouts.html
否则,你可能会幸运地在 http://cocoapods.org/ 上找到一个
我快速搜索了一下,一无所获。
正如理查达斯所建议的...
这是一个教程:
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
祝你好运!
如何实现自定义 UICollectionFlowLayout
要了解如何开始使用自定义集合视图布局,我强烈推荐这个 link: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;
}
我通过继承 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;
}
我正在尝试将序列更改为在 collectionview 中呈现的单元格。默认情况下,在垂直布局中,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
并试图操纵它的方法但是没有成功
注意:所有项目只能在一个部分加载
为了实现这一点,您将需要构建自己的集合视图布局。首先阅读此处的文档:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCustomLayouts/CreatingCustomLayouts.html
否则,你可能会幸运地在 http://cocoapods.org/ 上找到一个 我快速搜索了一下,一无所获。
正如理查达斯所建议的...
这是一个教程: 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
祝你好运!
如何实现自定义 UICollectionFlowLayout
要了解如何开始使用自定义集合视图布局,我强烈推荐这个 link: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;
}
我通过继承 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;
}