简体   繁体   English

UIScrollView优于UITableView和UICollectionView的优势

[英]Advantage of UIScrollView over UITableView and UICollectionView

I'm working on a MAC tool where you can see the current viewcontroller UIElements hierarchy of an iOS app. 我正在使用一种MAC工具,您可以在其中看到iOS应用程序的当前viewcontroller UIElements层次结构。 I use to test it with so many third party apps. 我曾经用很多第三方应用程序对其进行测试。 As per my knowledge to list something in horizontal or vertical most of the time developer use UITableView or UICollectionView . 据我所知,大多数时候开发人员都会使用UITableViewUICollectionView来水平或垂直列出某些UICollectionView

While testing some of the standard apps (especially e-commerce) are using UIScrollView with UIView as subview and added gesture on it. 在测试某些标准应用程序(尤其是电子商务)时,将UIScrollViewUIView用作子视图并在其上添加了手势。 What i'm trying to understand is, why these apps are not using tableview or collection to list something? 我想了解的是,为什么这些应用程序没有使用tableview或collection列出某些内容? and what is the reason behind this? 这背后的原因是什么?

This are some of things I always prefer UITableView and UICollectionView. 我一直喜欢UITableView和UICollectionView。

  1. One of most advantage of UITableView and UICollectionView is reusability . UITableView和UICollectionView的最大优势之一是可重用性
  2. When comes to autolayout its easy to handle UITableView and UICollectionView over UIScrollView 当涉及到自动布局时,通过UIScrollView可以轻松处理UITableView和UICollectionView
  3. Indexing of views 视图索引
  4. Delegate methods support 委托方法支持
  5. Reload(Update) Views its easy compare to UIScrollView 重新加载(更新)查看它与UIScrollView的比较

One thing I miss is Paging in UITableView and UICollectionView as its so smooth in UIScrollView 我想念的一件事是在UITableView和UICollectionView中进行分页 ,因为它在UIScrollView中如此平滑

(1) Advantage of using UITableView or UICollectionView against UIScrollView is for memory. (1)针对UIScrollView使用UITableView或UICollectionView的优点是用于内存。

For example if you have 100 items then you have to keep adding below in UIScrollView, so it 100 items data will be in memory. 例如,如果您有100个项目,则必须继续在UIScrollView中添加以下内容,因此它的100个项目数据将存储在内存中。

In UITableView or UIcollectionView there is dequeueReusableCellWithIdentifier that keeps only those items in memory which are visible. 在UITableView或UIcollectionView中,有dequeueReusableCellWithIdentifier只能将那些可见的项保留在内存中。 When you scroll up / down though items are not visible will be in memory. 当您向上/向下滚动时,虽然看不见的项目仍将保留在内存中。

(2) Delegate methods - NoOfSections, NoOfRows, DidSelectedRow etc - Though things are not available in UIScrollView.So you have to manage through coding. (2)委托方法-NoOfSections,NoOfRows,DidSelectedRow等-虽然UIScrollView中没有可用的东西,所以您必须通过编码进行管理。

UICollectionView Class :- UICollectionView类别:-

The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts. UICollectionView类管理数据项的有序集合,并使用可自定义的布局来呈现它们。 Collection views provide the same general function as table views except that a collection view is able to support more than just single-column layouts. 集合视图提供与表视图相同的常规功能,除了集合视图不仅可以支持单列布局,还可以支持更多的功能。 Collection views support customizable layouts that can be used to implement multi-column grids, tiled layouts, circular layouts, and many more. 集合视图支持可自定义的布局,可用于实现多列网格,平铺布局,圆形布局等。 You can even change the layout of a collection view dynamically if you want. 如果需要,您甚至可以动态更改集合视图的布局。

UITableView Class:- UITableView类别:-

A table view displays a list of items in a single column. 表格视图在单个列中显示项目列表。 UITableView is a subclass of UIScrollView, which allows users to scroll through the table, although UITableView allows vertical scrolling only. UITableView是UIScrollView的子类,尽管UITableView仅允许垂直滚动,但它允许用户滚动表。 The cells comprising the individual items of the table are UITableViewCell objects; 组成表格中各个项目的单元格是UITableViewCell对象; UITableView uses these objects to draw the visible rows of the table. UITableView使用这些对象绘制表的可见行。 Cells have content—titles and images—and can have, near the right edge, accessory views. 单元具有内容(标题和图像),并且在右边缘附近可以具有附件视图。 Standard accessory views are disclosure indicators or detail disclosure buttons; 标准附件视图是公开指示符或详细信息公开按钮。 the former leads to the next level in a data hierarchy and the latter leads to a detailed view of a selected item. 前者导致数据层次结构中的下一个层次,而后者则导致所选项目的详细视图。 Accessory views can also be framework controls, such as switches and sliders, or can be custom views. 辅助视图也可以是框架控件,例如开关和滑块,也可以是自定义视图。 Table views can enter an editing mode where users can insert, delete, and reorder rows of the table. 表格视图可以进入编辑模式,用户可以在其中插入,删除和重新排序表格的行。

People mainly use the UICollectionview for creating types of UIs with multiple images shown in a grid. 人们主要使用UICollectionview来创建具有网格中显示的多个图像的UI类型。 This would have complex logic using UITableView, but with UICollectionview , it would be easy. 使用UITableView会具有复杂的逻辑,但是使用UICollectionview会很容易。

UITableView has many advantages over UIScrollView like: 与UIScrollView相比,UITableView具有许多优点,例如:

  • When using auto layout, UITableView is more reusable. 使用自动布局时,UITableView更可重用。
  • List item 项目清单

Memory management is better in case of UITableView. 对于UITableView,内存管理更好。

One advantage of UIScrollView over UITableView/UICollectionView/AutoLayout is smooth scroll performance. 与UITableView / UICollectionView / AutoLayout相比,UIScrollView的一项优势是流畅的滚动性能。

Recently, I needed to reimplement some old news app which used UIScrollView to display news content. 最近,我需要重新实现一些使用UIScrollView来显示新闻内容的旧新闻应用程序。 App would create all views and add them to UIScrollView. 应用程序将创建所有视图并将其添加到UIScrollView。 That would consume ca 15-20 MB of RAM memory. 这将消耗大约15-20 MB的RAM内存。

The new design I had to implement was more complex, but could be achieved either via UIScrollView or UITableView and UICollectionView combination. 我必须实现的新设计更加复杂,但是可以通过UIScrollView或UITableView和UICollectionView的组合来实现。 I went with UITableView & UICollectionView and Auto Layout. 我去了UITableView&UICollectionView和自动布局。 After finishing implementation, performance was TERRIBLE. 完成实施后,性能非常糟糕。 Previous UIScrollView implementation was perfect 60fps on iPhone 4S. 以前的UIScrollView实施在iPhone 4S上是完美的60fps。 With the new implementation I would get dropped frames even on iPhone 6S. 使用新的实现,即使在iPhone 6S上,我也会掉帧。 I optimized images so that they're resized on background so that there are no misaligned images and I also minimized blended layers passes. 我优化了图像,以便在背景上调整图像的大小,以确保没有未对齐的图像,并且还将混合图层的通过最小化。 That helped a bit, but still far from perfect. 这有所帮助,但还远远不够完美。

I ended up dropping AutoLayout in many places and preloading all UITableView cells and keeping them in an NSArray so cells can be returned immediately without configuring (since that takes time). 我最终在很多地方删除了AutoLayout并预加载了所有UITableView单元,并将它们保留在NSArray中,以便可以立即返回单元而无需进行配置(因为这需要时间)。 With this of course, table view cell reusability went down the toilet, but scroll performance improved quite a bit even on iPhone 4S. 当然,有了此功能,表格视图单元的可重用性便大打折扣,但是即使在iPhone 4S上,滚动性能也有了很大提高。

Conclusion: sometimes, UIScrollView and manual frame calculation is worth the effort if your whole view is not too large so that it can fit into memory relatevly easily. 结论:有时,如果您的整个视图不太大,那么UIScrollView和手动框架计算值得您付出努力,以便可以轻松地将其轻松地放入内存。 Often, UITableView & UICollectionView & Auto Layout can prove expensive in terms of dropped frames while scrolling. 通常,就滚动时丢帧而言,UITableView&UICollectionView&自动布局可能会变得很昂贵。 End users won't care how you implemented it, but they will complain about app not being smooth. 最终用户不会在意您如何实现它,但是他们会抱怨应用程序不流畅。

Of course, rarely do you ever need this. 当然,您很少需要此功能。 ALWAYS stick first to UITableView & AutoLayout (or UICollectionView & AutoLayout if needed) since they make life easier for many reasons (easy various screen sizes support including rotations), but do keep in mind that there is a UIScrollView should a black day come and a need arise. 始终首先坚持使用UITableView&AutoLayout(如果需要,还可以坚持使用UICollectionView&AutoLayout),因为它们出于多种原因(使各种屏幕尺寸轻松支持,包括旋转)使生活变得更加轻松,但是请记住,如果天气不好,应该使用UIScrollView。需要出现。

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

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