简体   繁体   English

将UICollectionView和UITableView放入iOS的UIScrollView中是一种好习惯吗?

[英]Is it a good practice to put UICollectionView and UITableView inside a UIScrollView in iOS?

I've googled questions like "Put a table view/collection view inside a scroll view", but all of those questions are asking about how to solve some of the unexpected behaviors when doing so, not many are asking if it's a good practice. 我已经用谷歌搜索“将表视图/集合视图放入滚动视图内”之类的问题,但是所有这些问题都在询问如何解决这样做时的某些意外行为,很少有人问这是否是一个好习惯。

I used to use react-native to develop mobile apps. 我曾经使用react-native开发移动应用程序。 They have this component called FlatList , sort of like UITableView in Swift. 他们有一个称为FlatList组件,有点像Swift中的UITableView I once put a FlatList inside a ScrollView and implemented the unlimited scroll, however, there were many strange things that happened: 我曾经将FlatList放在ScrollView并实现了无限滚动,但是,发生了很多奇怪的事情:

  1. I haven't even scrolled to the bottom, but the FlatList keeps rendering more data, as if it has already reached the end, which caused memory leak after a while 我什至没有滚动到底部,但是FlatList继续渲染更多数据,好像已经到达末尾了,这导致一段时间后内存泄漏

  2. When I tried to get the current scroll position and do some animation based on it, it always gave me the wrong scroll position 当我尝试获取当前的滚动位置并基于其进行一些动画处理时,它总是给我错误的滚动位置

  3. etc ... 等...

I wrote the same thing in Swift, none of the issues listed above came up. 我在Swift中写了同样的东西,上面没有列出任何问题。 However, I want to ask, if there could be any potential problems when doing so? 但是,我想问一问, 这样做有没有潜在的问题?

To help you understand why I would want to put a table view & collection view inside a scroll view, look at this pic (If you could suggest some other way to do the same thing without having table view/collection view inside a scroll view, that'd be great!) 为了帮助您理解为什么我希望将表格视图和集合视图放在滚动视图中,请查看此图片(如果您可以提出一些其他方法来做同样的事情而无需在表格视图/集合视图中使用表格视图/集合视图,那简直太好了!)

在此处输入图片说明

Update 更新资料

I put everything inside a scroll view because I want to disable the keyboard on drag when user is typing in text view 我将所有内容都放在滚动视图中,因为当用户在文本视图中键入内容时,我想在拖动时禁用键盘

TableView and CollectionView inherit from ScrollView. TableView和CollectionView从ScrollView继承。 So that behaviour you mention is already on those components. 因此,您提到的行为已经存在于那些组件上。

The 'best' practice would be to have only 1 CollectionView (without ScrollView nor TableView) and create your own CollectionViewLayout so you can define different layout and scrolling for each of your sections, but that could get a bit too complex as you would have to implement those behaviours yourself. 最好的做法是只有1个CollectionView(没有ScrollView或TableView)并创建自己的CollectionViewLayout,以便为每个部分定义不同的布局和滚动方式,但这可能会变得有些复杂,因为您必须自己实施这些行为。

Another approach would be to have a main CollectionView, with another CollectionView inside for each of your sections (1 for the photos, 1 for the 'add location', 'tag person', etc). 另一种方法是拥有一个主CollectionView,在每个部分的内部都具有另一个CollectionView(1个用于照片,1个用于“添加位置”,“标签人”等)。

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

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