给定一个具有流式布局的 UICollectionView
,计算 Collection View 的列数(垂直方向)或行数(水平方向)的最简单方法是什么?
最佳答案
我的做法(假设所有元素具有相同的大小):
- (void) countRowsAndColumns
{
NSArray *layouts = [collectionViewLayout layoutAttributesForElementsInRect:self.collectionView.bounds];
NSMutableSet *columns = [NSMutableSet set];
NSMutableSet *rows = [NSMutableSet set];
for (UICollectionViewLayoutAttributes *layout in layouts)
{
if (layout.representedElementCategory != UICollectionElementCategoryCell) continue;
CGFloat x = layout.frame.origin.x;
[columns addObject:@(x)];
CGFloat y = layout.frame.origin.y;
[rows addObject:@(y)];
}
_columnCount = columns.count;
_rowCount = rows.count;
}
关于ios - UICollectionViewFlowLayout 的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15710461/