UICollectionViewFlowLayout 的行和列 [英] Rows and columns of UICollectionViewFlowLayout

查看:34
本文介绍了UICollectionViewFlowLayout 的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

我的hackish方式(假设所有元素具有相同的大小):

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

这篇关于UICollectionViewFlowLayout 的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆