简体   繁体   English

收藏集视图布局

[英]Collection View layout

Is it possible to create a 3 column newspaper like layout with collection view? 是否可以创建带有收藏夹视图的三栏式报纸布局?

I tried using 3 table views and it works but is very laggy. 我尝试使用3个表视图,它可以工作,但是非常落后。 So if collection views are a native solution I think it's better to use them. 因此,如果集合视图是本机解决方案,我认为最好使用它们。 Do they fit my task? 它们适合我的任务吗?

The main problem is that all cells are filled from left to right: 主要问题是所有单元格都是从左到右填充的:

1 2 3
4 5 6

And I need: 我需要:

1 5 9
2 6 10
3 7 11
4 8 12

Yes, I think that a collectionview would suit your needs. 是的,我认为collectionview将适合您的需求。 You will probably have to use a custom layout. 您可能必须使用自定义布局。 Check out "introduction to collectionviews" and "advanced layout with collection views" on the WWDC 2012 videos. 在WWDC 2012视频中查看“ collectionviews简介”和“ collection view的高级布局”。 By using a custom layout you can order the cells any way you like. 通过使用自定义布局,您可以按自己喜欢的方式对单元进行排序。

I suggest you to reorder your data source. 我建议您重新排序数据源。

EDIT: 编辑:

NSArray *oldArr = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11"];
NSMutableArray *newArr = [NSMutableArray arrayWithCapacity:oldArr.count];

int row = ceilf(oldArr.count / 3.f);
int remainder = oldArr.count % 3;

for (int i = 0; i < row; i++) {
    [newArr addObject:[oldArr objectAtIndex:i]];

    if (i == (row - 1) && remainder == 1)
        continue;

    [newArr addObject:[oldArr objectAtIndex:i + row]];

    if (i == (row - 1) && remainder == 2)
        continue;

    [newArr addObject:[oldArr objectAtIndex:i + row * 2]];
}

result: 结果:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, = > 1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8

formatting print: 格式化打印:

1  2  3
4  5  6
7  8  9
10 11

to

1  5  9
2  6  10
3  7  11
4  8

You can by changing the scroll direction to horizontal. 您可以将滚动方向更改为水平。 That way you'll have it ordered like you want, and scrolling makes the most sense. 这样一来,您就可以按自己的意愿对其进行排序,而滚动则最有意义。

If you wanna do that with vertical scrolling, you'll have to write your own subclass of the UICollectionViewFlowLayout . 如果要通过垂直滚动进行此操作,则必须编写自己的UICollectionViewFlowLayout子类。 But would make the user experience really bad. 但这会使用户体验真的很差。

Ok, I've finished with fixing my 3 table view layout. 好的,我已经完成了3表格视图布局的修复。 Instead of loading cells from XIB I've created programmatically. 我不是通过XIB加载单元,而是通过编程方式创建的。 This gave me huge increase in performance. 这使我的性能大大提高。 Thank you for the answers guys. 谢谢你们的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM