UICollectionViewFlowLayout的行和列

给定一个具有stream布局的UICollectionView ,计算集合视图的列数(垂直方向)或行数(水平方向)的最简单方法是什么?

我做这件事的方式(假定所有元素都有相同的大小):

 - (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; }