ios - UICollectionViewFlowLayout 实现

标签 ios uicollectionview uicollectionviewlayout

我正在尝试完成以下任务,我已经使用 UIScrollview 轻松完成了此任务,但我最近一直在尝试 UICollectionView (我知道我玩游戏已经很晚了)并且很想知道是否可以这样做我想要什么,我必须实现一个自定义布局,或者如果 FlowLayout 已经为我做到了这一点。

基本上,如果您查看附件,您会注意到滚动可以垂直和水平发生,行一直超出 UICollectionView 高度。对于超出 Collection View 宽度的列也会发生同样的情况。

这可以用 Flowlayout 来做吗?

Sample

最佳答案

我就是这样做的。

#define space 5
#import "MultpleLineLayout.h"

@implementation MultpleLineLayout { // a subclass of UICollectionViewFlowLayout
    NSInteger itemWidth;
    NSInteger itemHeight;
}

-(id)init {
    if (self = [super init]) {
        itemWidth = 60;
        itemHeight = 60;
    }
    return self;
}

-(CGSize)collectionViewContentSize {
    NSInteger xSize = [self.collectionView numberOfItemsInSection:0] * (itemWidth + space); // "space" is for spacing between cells.
    NSInteger ySize = [self.collectionView numberOfSections] * (itemHeight + space);
    return CGSizeMake(xSize, ySize);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path {
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    attributes.size = CGSizeMake(itemWidth,itemHeight);
    int xValue = itemWidth/2 + path.row * (itemWidth + space);
    int yValue = itemHeight + path.section * (itemHeight + space);
    attributes.center = CGPointMake(xValue, yValue);
    return attributes;
}


-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    NSInteger minRow =  (rect.origin.x > 0)?  rect.origin.x/(itemWidth + space) : 0; // need to check because bounce gives negative values  for x.
    NSInteger maxRow = rect.size.width/(itemWidth + space) + minRow;
    NSMutableArray* attributes = [NSMutableArray array];
    for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) {
        for (NSInteger j=minRow ; j < maxRow; j++) {
            NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
    }
    return attributes;
}

我的数据源是一个数组数组,每个内部数组提供单个行的数据。

编辑后:

我的收藏 View 向两个方向滚动。这是我在 viewDidload 中进行设置的内容:

- (void)viewDidLoad {
    self.theData = @[@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"], @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],@[@"Z0",@"Z1",@"Z2",@"Z3",@"Z4",@"Z5",@"Z6",@"Z7",@"Z8",@"Z9",@"Z10",@"Z11",@"Z12",@"Z13",@"Z14",@"Z15",@"Z16",@"Z17",@"Z18",@"Z19",@"Z20"]];
    MultpleLineLayout *layout = [[MultpleLineLayout alloc] init];
    self.collectionView = [[RDCollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[DataCell class] forCellWithReuseIdentifier:@"DataCell"];
    [self.collectionView reloadData];
}

关于ios - UICollectionViewFlowLayout 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17732938/

相关文章:

ios - 遍历 UICollectionView 中的所有单元格

ios - 设置 Collection View 单元格的动态宽度和高度

objective-c - UICollectionView 部分标题崩溃 iOS 8

ios xcode GPUimage 视频录制和静态图像捕捉

html - iOS 需要双击一个简单的链接元素

ios - UICollectionView 与 UITextView 非常慢

uicollectionview - 在Storyboard中设置UICollectionViewLayout类

ios - Xcode中如何知道Objective-C语言版本

ios - iOS 中的 DBL_EPSILON 问题

drawrect - 使用drawRect绘制的collectionView :cellForItemAtIndexPath mixes up images,: