Given a UICollectionView
with flow layout, what's the simplest way to calculate the number of columns (vertical direction) or rows (horizontal direction) of the collection view?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
My hackish way of doing it (assumes all elements have the same size):
- (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;
}