UICollectionViewFlowLayout的行和列

时间:2013-03-29 19:50:50

标签: ios uicollectionview uicollectionviewlayout

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

1 个答案:

答案 0 :(得分:1)

我这样做的hackish方式(假设所有元素都有相同的大小):

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