简体   繁体   English

与Swift在同一ViewController中的TableView和CollectionView

[英]TableView and CollectionView in the same ViewController with Swift

I'm building my application with Swift and I want to set up TableView and CollectionView in the same ViewController . 我正在用Swift构建我的应用程序,我想在同一ViewController设置TableViewCollectionView

它看起来应该像这样(此处为图片)

The only answer I founded is to setup my CollectionView in the TableViewCell of the TableView . 我建立的唯一答案是在TableViewCellTableView设置我的CollectionView

I did it but it scrolls separately. 我做到了,但它单独滚动。

The solution I found is to disable CollectionView scrolling and then configure CollectionView 's height constraint programmatically by summing heights of the cells. 我发现的解决方案是禁用CollectionView滚动,然后通过对单元格的高度求和以编程方式配置CollectionView的高度约束。 It isn't perfect at all because cells become non reusable (250+). 它根本不是完美的,因为细胞变得不可重用(超过250个)。

I'm in desperation because I have seen this setup in many other apps and I think that answer is so simple but I can not find it anywhere. 我很绝望,因为我已经在许多其他应用程序中看到了此设置,我认为答案是如此简单,但我在任何地方都找不到。

Can anyone please help me? 谁能帮帮我吗? Thank you a lot! 非常感谢!

Another option is to set it up as a single collection view with two sections, one that looks and lays out as a tableview and the other that has the more collection view appearance. 另一种选择是将其设置为具有两个部分的单个集合视图,一个部分的外观和布局为表格视图,另一个部分具有更多的集合视图外观。

Or set it up as a single table view with two sections, one of which displays two side-by-side images per line (this is the old school way of creating this layout.) 或将其设置为具有两个部分的单个表格视图,其中一个部分每行显示两个并排图像(这是创建此布局的传统方法。)

Lots of ways to go about it, depending on what you want to play with and what you want to learn :) 有很多解决方法,具体取决于您想玩什么和想学什么:)

You can put a UITableView and UICollectionView inside a UIViewController , and implement UITableViewDelegate , UITableViewDataSource and UICollectionViewDataSource . 您可以将UITableViewUICollectionView放在UIViewController ,并实现UITableViewDelegateUITableViewDataSourceUICollectionViewDataSource

Keys: 按键:

  1. Use AutoLayout to make sure tableView has only two rows, and collectionView takes the rest of area. 使用AutoLayout确保tableView仅具有两行,而collectionView占用其余区域。

  2. Make sure tableView.scrollEnabled = false 确保tableView.scrollEnabled = false

Here is a sample, https://www.dropbox.com/s/v6yc40udfk8mqx5/FlexTableCellHeight.zip?dl=0 这是一个示例, https://www.dropbox.com/s/v6yc40udfk8mqx5/FlexTableCellHeight.zip?dl=0

Set up a blank ViewController. 设置一个空白的ViewController。 Then place two ContainmentViewControllers inside of the ViewController. 然后在ViewController内放置两个ContainmentViewController。 Size them however you want based on how much screen you want to the be tableview and how much you want to be collectionview. 根据您想要成为TableView的屏幕和您想要成为CollectionView的屏幕的大小来调整它们的大小。 Then create segues from each containmentViewController to your tableviewcontroller and collectionviewcontroller using embed. 然后使用embed从每个containmentViewController到您的tableviewcontroller和collectionviewcontroller创建序列。 This will allow both to be on screen at once and scroll separately. 这将允许两者同时显示在屏幕上并分别滚动。

If you do not need to use a TableViewController and can get away with just a tableview you can also just place one containmentviewcontroller below your tableview and create the embed segue to your collectionviewcontroller. 如果您不需要使用TableViewController并且可以只使用一个Tableview,则还可以在TableView下方放置一个containmentviewController,并为您的collectionviewController创建嵌入的序列。

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

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