shr*_*ter 4 ios ios6 uicollectionview
UICollection 视图会根据每个部分的项目数和每个单元格的大小自动调整行数。
那么有没有办法获取 UICollectionView 中的行数?
例如:如果我有一个 31 天的日历,可以自动放入 n 行。我如何获得这个 'n' 的值?
您可以在布局后获取项目的位置 [myCollectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:]
如果您不知道项目的尺寸,或者不想假设您知道,一种方法是迭代集合中的项目并寻找项目 Y 位置的阶跃变化。显然,这仅在您使用基于网格的布局时才有效。
这是诀窍:
NSInteger totalItems = [myCollectionView numberOfItemsInSection:0];
// How many items are there per row?
NSInteger currItem;
CGFloat currRowOriginY = CGFLOAT_MAX;
for (currItem = 0; currItem < totalItems; currItem++) {
UICollectionViewLayoutAttributes *attributes =
[collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:
[NSIndexPath indexPathForItem:currItem inSection:0]];
if (currItem == 0) {
currRowOriginY = attributes.frame.origin.y;
continue;
}
if (attributes.frame.origin.y > currRowOriginY + 5.0f) {
break;
}
}
NSLog(@"new row started at item %ld", (long)currItem);
NSInteger totalRows = totalItems / currItem;
NSLog(@"%ld rows", (long)totalRows);
Run Code Online (Sandbox Code Playgroud)
如果你做的知道你的项目的尺寸,你可以得到的位置最后一个项目用
NSInteger totalItems = [self.timelineCollectionView numberOfItemsInSection:0];
NSIndexPath lastIndex = [NSIndexPath indexPathForItem:totalItems - 1 inSection:0];
UICollectionViewLayoutAttributes *attributes =
[myCollectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:lastIndex];
// Frame of last item is now in attributes.frame
Run Code Online (Sandbox Code Playgroud)
然后取最后一项的尺寸并除以您已知的行高。不要忘记考虑任何标题或间距。这些属性也可从UICollectionViewFlowLayout.
拉出流布局
UICollectionViewFlowLayout *myFlowLayout = (UICollectionViewFlowLayout*)myCollectionView.collectionViewFlowLayout;
Run Code Online (Sandbox Code Playgroud)
看看
myFlowLayout.headerReferenceSize
myFlowLayout.minimumLineSpacing
Run Code Online (Sandbox Code Playgroud)
等等。
| 归档时间: |
|
| 查看次数: |
7368 次 |
| 最近记录: |