[英]Custom collection flow layout for UICollectionView in Swift
i'm learning iOS with Swift, and now i wanted to create simple messenger app (very very simple version of WhatsApp). 我正在使用Swift学习iOS,现在我想创建一个简单的Messenger应用(WhatsApp的非常简单的版本)。 I've looked through all collection view video tutorials, but i still cannot understand how to create a layout, where cells will be on two sides of the screen (two cells couldn't be in one row). 我已经看过所有收藏夹视图视频教程,但仍然无法理解如何创建布局,在该布局中,单元格将位于屏幕的两侧(两个单元格不能排成一行)。 So as far as i understand, i should go with flow layout. 据我了解,我应该采用流程布局。 I override layoutAttributesForElementsInRect like this: 我像这样覆盖layoutAttributesForElementsInRect:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
let attributes = super.layoutAttributesForElementsInRect(rect) as! [UICollectionViewLayoutAttributes]
for attribute in attributes {
attribute.frame.origin.x = collectionView!.bounds.width - attribute.frame.width
}
return attributes
}
And now my cells lay under each other (for example i have 50 cells, but i could see only 10 with 5 cells in depth). 现在我的单元格彼此重叠(例如,我有50个单元格,但我只能看到10个单元格,深度为5个单元格)。
EDIT: As you can see, 5 cells lay under each other in each row, so totally i got 10 rows, not 50. 编辑:正如您所看到的,每行下面都有5个单元格,因此我总共得到10行,而不是50行。
And i want to see all 50 cells from right side. 我想从右侧看到所有50个单元。
So could somebody give any advice how can i implement this layout. 因此,有人可以提供任何建议如何实现此布局。 Thanks for any help! 谢谢你的帮助!
Perhaps a bit late, but hopefully useful. 也许有点晚,但希望有用。
1) If you want to display one column of cells, why not go with a UITableView? 1)如果要显示一列单元格,为什么不使用UITableView?
2) Make two cells, one 'me' cell and one 'other person' cell. 2)制作两个单元格,一个为“我”单元,另一个为“其他人”单元。 They look different: Different colors perhaps but the one cell has the Labels aligned to the Left, the other to the right. 它们看起来不同:也许是不同的颜色,但是一个单元格的标签左对齐,另一个单元右对齐。 When using a UICollectionView, the cells should be wider than Half the screen, so that they are automagically placed in one column. 使用UICollectionView时,单元格的宽度应大于屏幕的一半,以便将它们自动放置在一列中。 No need to override layoutAttributesForElementsInRect in a custom FlowLayout subclass. 无需在自定义FlowLayout子类中重写layoutAttributesForElementsInRect。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.