ios 何时使用 UICollectionView 而不是 UITableView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23078847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:57:40  来源:igfitidea点击:

When to use UICollectionView instead of UITableView?

iosobjective-cuitableviewuicollectionview

提问by wz366

I found that UICollectionViewis like an upgraded version of UITableViewintroduced in iOS6, but when should I choose UICollectionViewinstead of UITableView?

我发现UICollectionView好像是UITableViewiOS6中引入的升级版,但是我什么时候应该选择UICollectionView而不是UITableView

There are still Apps using UITableView, if UICollectionViewcan do anything UITableViewcan do , why people still use UITableView? Is there a difference as far as performance is concerned?

还有应用程序在使用UITableView,如果UICollectionView可以做任何UITableView事情,为什么人们还在使用UITableView?就性能而言有区别吗?

Thanks!

谢谢!

采纳答案by Nitin Gohel

That depends on the requirements. How the application flows determines which type of UI to integrate into the application.

这取决于要求。应用程序的流动方式决定了将哪种类型的 UI 集成到应用程序中。

People mainly use the UICollectionviewfor creating types of UIs with multiple images shown in a grid. This would have complex logic using UITableView, but with UICollectionview, it would be easy.

人们主要使用UICollectionview来创建在网格中显示多个图像的 UI 类型。使用 会有复杂的逻辑UITableView,但是使用UICollectionview会很容易。

When using UICollectionview, you don't need to set buttons with tags or other things by getting selected items values. You can simply get -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPathand in UITableViewDelegate:

使用时UICollectionview,您不需要通过获取所选项目值来设置带有标签或其他东西的按钮。你可以简单地得到-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath和在UITableViewDelegate

`-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`

You get the selected row instead of the item, so for creating grid or modified items, using UICollectionviewis best.

您将获得所选行而不是项目,因此对于创建网格或修改项目,UICollectionview最好使用。

For the listing details of each item, people use UITableViewbecause it shows more info on each item.

对于每个项目的列表详细信息,人们使用UITableView它是因为它显示了每个项目的更多信息。

Apple Docs:

苹果文档:

UICollectionView Class Reference

UICollectionView 类参考

The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts. 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.

UICollectionView 类管理有序的数据项集合并使用可自定义的布局呈现它们。集合视图提供与表视图相同的通用功能,除了集合视图能够支持的不仅仅是单列布局。集合视图支持可自定义的布局,可用于实现多列网格、平铺布局、圆形布局等等。如果需要,您甚至可以动态更改集合视图的布局。

UITableView Class Reference

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. The cells comprising the individual items of the table are UITableViewCell objects; UITableView uses these objects to draw the visible rows of the table. 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.

表视图在单列中显示项目列表。UITableView 是 UIScrollView 的子类,它允许用户滚动表格,尽管 UITableView 只允许垂直滚动。组成表格各个项目的单元格是 UITableViewCell 对象;UITableView 使用这些对象来绘制表格的可见行。单元格有内容——标题和图像——并且可以在靠近右边缘的地方有附属视图。标准附件视图是披露指示器或细节披露按钮;前者通向数据层次结构中的下一个级别,后者通向所选项目的详细视图。辅助视图也可以是框架控件,例如开关和滑块,也可以是自定义视图。表视图可以进入编辑模式,用户可以在其中插入、删除和重新排序表的行。

回答by n13

Here's my criteria:

这是我的标准:

  • If a UITableView can do it, use it

  • If a UITableView needs lots of code to do it or can't do it at all, use UICollectionView.

  • 如果 UITableView 可以做到,请使用它

  • 如果 UITableView 需要大量代码来完成或根本无法完成,请使用 UICollectionView。

You have to consider the restrictions on UITableView before making a decision: It's a single column. And you can only customize the cells, but not section backgrounds and such. So if you have a straight-up list of things with no extra frills - that looks like a bog standard iOS view, basically - then use UITableview. If you have custom insets, or a border around each section, use UICollectionView.

在做出决定之前,您必须考虑对 UITableView 的限制:它是单列。而且您只能自定义单元格,而不能自定义部分背景等。因此,如果您有一个没有多余装饰的直接列表 - 基本上看起来像沼泽标准的 iOS 视图 - 然后使用 UITableview。如果您有自定义插图或每个部分的边框,请使用 UICollectionView。

I'm actually considering UICollectionView for all things simply because it's very expensive when you start developing your view as a table view, then later find out it can't do that one thing that you need it to do. 1st hand experience ;)

我实际上正在考虑将 UICollectionView 用于所有事情,因为当您开始将视图开发为表视图时它非常昂贵,然后发现它无法完成您需要它做的一件事。第一手经验;)

Edit after even more experience with the two: Disregard that last paragraph. UICollectionView requires a lot of boilerplate code to make it work like a UITableView. Use UICollectionView only when really needed. ;)

在对两者进行更多经验之后进行编辑:忽略最后一段。UICollectionView 需要大量样板代码才能使其像 UITableView 一样工作。仅在真正需要时才使用 UICollectionView。;)

回答by Andrew Ebling

For simple lists and forwards/backwards navigtaion, use UITableView.

对于简单的列表和向前/向后导航,请使用UITableView.

If you need a high degree of customisability, use UICollectionView.

如果您需要高度的可定制性,请使用UICollectionView.

Generally speaking, in software development, it's best to choose the approach which represents "The Simplest Possible Thing".

一般来说,在软件开发中,最好选择代表“最简单的事情”的方法。

回答by Muhammad Waqas Bhati

According to my point of view main difference between collectionView and tableView is that

根据我的观点,collectionView 和 tableView 之间的主要区别在于

TABLEVIEW --> show list of items in only one column.

COLLECTION-VIEW -->show list of items in multiple column.

TABLEVIEW --> 仅显示一列中的项目列表。

集合视图 --> 显示多列中的项目列表。

Hope it will help you.

希望它会帮助你。

回答by BricoleurDev

If you choose UITableView for iPhone, make sure you have considered your iPad strategy first. If you want an iPad-specific layout, you may want that single-column layout to become a grid.

如果您为 iPhone 选择 UITableView,请确保您首先考虑了您的 iPad 策略。如果您想要特定于 iPad 的布局,您可能希望该单列布局成为网格。

回答by Chris Van Buskirk

Although it's not required, I always use a collectionview. That way I can easily adapt how my collections are presented for differing resolutions. A plus is that it's ready to quickly add new types of cells when refactoring in the future.

虽然不是必需的,但我总是使用 collectionview。这样我就可以轻松调整我的收藏在不同分辨率下的呈现方式。一个优点是它可以在将来重构时快速添加新类型的单元格。

I see no point of tableviews. It's very simple to use a collection view to represent a table. IMO.

我看不到 tableviews 的意义。使用集合视图来表示表格非常简单。海事组织。

回答by thoughtbreaker

Its totally dependent on how your data to be shown. As mentioned by many above, if you require only single set of data and that too not complex, go for UITableViewelse use UICollectionView.

它完全取决于您的数据如何显示。正如上面许多人所提到的,如果您只需要一组数据并且太不复杂,请使用UITableViewelse UICollectionView

UICollectionViewis customization friendly.

UICollectionView定制友好。

If you are dealing with multiple cell heights or so, then go for UICollectionView.

如果您要处理多个单元格高度左右,请选择UICollectionView.

回答by Matthew Cawley

From my personal experience the two elements should only be compared loosly.

根据我的个人经验,这两个元素只能粗略地进行比较。

TableView

表视图

A TableView is a UI element designed for showing data in a list format. There is certain functionality that comes as standard with a UITableView, such as:

TableView 是一个 UI 元素,旨在以列表格式显示数据。UITableView 有一些标准功能,例如:

  • Accessory View
  • Cell Selection Style
  • Editting Style (Delete and edit buttons).
  • 附件视图
  • 单元格选择样式
  • 编辑样式(删除和编辑按钮)。

The above elements enhance the usability of data when displaying and interacting in a list format. Such as viewing emails.

当以列表格式显示和交互时,上述元素增强了数据的可用性。比如查看邮件。

CollectionView

集合视图

A CollectionView is a UI element designed for showing content using a custom layout (usually anything that isn't a list). CollectionViews improve functionality of displaying data in completely bespoke layout styles and also dynamically changing layouts on the fly. Some examples are:

CollectionView 是一个 UI 元素,设计用于使用自定义布局(通常不是列表的任何内容)显示内容。CollectionViews 改进了以完全定制的布局样式显示数据的功能,还可以动态地动态更改布局。一些例子是:

  • Horizonal Lists
  • Photo Galleries
  • Thumbnail views
  • Carousels
  • Dials
  • Laying out elements on a map
  • etc.
  • 水平列表
  • 照片画廊
  • 缩略图视图
  • 旋转木马
  • 表盘
  • 在地图上布置元素
  • 等等。

CollectionViews also allow for multiple selections.

CollectionViews 还允许进行多项选择。

Conclusion

结论

As you can see from the above, both have completely different use cases and are designed for enhancing the development and usability of their own specific data sets.

从上面可以看出,两者都有完全不同的用例,旨在增强各自特定数据集的开发和可用性。

If you are looking at displaying anything in a list style with the followin interactions: - Adding - Deleting - Re-ordering Then a UITableView will simplify this process by providing the support straight out of the box.

如果您希望通过以下交互以列表样式显示任何内容: - 添加 - 删除 - 重新排序 那么 UITableView 将通过直接提供开箱即用的支持来简化此过程。

Anything else, you should leverage the benefits of CollectionView as you have more flexibility.

其他任何事情,您都应该利用 CollectionView 的好处,因为您具有更大的灵活性。

回答by Bhumesh Purohit

Both are depends on the requirements. Table Views also have support for a variety of editing scenarios. This support has not been implemented in the Collection View classes. If you are converting from a Table View that relies on these methods, expect to do a little extra heavy lifting in the Collection View. Collection View section headers can be placed anywhere within the view. and UITableView don't need to set buttons with tags or other things by getting selected items values.

两者都取决于要求。表视图还支持各种编辑场景。集合视图类中尚未实现此支持。如果您从依赖这些方法的表视图转换,期望在集合视图中做一些额外的繁重工作。集合视图部分标题可以放置在视图内的任何位置。和 UITableView 不需要通过获取选定项目的值来设置带有标签或其他东西的按钮。

回答by Stephen J

In practice, everyone uses UICollectionView that I've come across, when they only need a UITableView. "It's one-dimensional. It goes up and down. Why are you adding unnecessary delegate methods for layout AND data?". I once spent an extra 2 hours helping a startup find out why their UICollectionViewCell got squished because the owner, who didn't read the Animations manual, nor HIG, nor the UICollectionView guide, decided to use it and add variable heights and anims. Needless to say, he gave himself a headache and much lost time on a non-business-critical issue he could have avoided by simply using a table cell, since there's no extra layout delegate + Nib.

在实践中,每个人都使用我遇到的 UICollectionView,当他们只需要一个 UITableView 时。“它是一维的。它会上升和下降。为什么要为布局和数据添加不必要的委托方法?”。我曾经额外花了 2 个小时帮助一家初创公司找出为什么他们的 UICollectionViewCell 被压扁了,因为没有阅读动画手册、HIG 和 UICollectionView 指南的所有者决定使用它并添加可变高度和动画。毋庸置疑,由于没有额外的布局委托 + Nib,他让自己头疼,并且在一个非业务关键问题上浪费了很多时间,他可以通过简单地使用表格单元格来避免。

Let me get this straight, I am all for UICollectionView's when your data and display need it. They're very powerful. But in practice, most people I've seen have been using them on lists.

让我直说吧,当您的数据和显示需要它时,我完全支持 UICollectionView。他们非常强大。但在实践中,我见过的大多数人都在列表中使用它们。

This brings up another flaw. They're also used on short, constant lists that won't change, ever. In this case, just make a Xib. Or write a custom view that stacks them. Why? Because you don't need the memory management for 5 sets of labels with a button or switch. If they might change, then yes, use a list. If you want physics, then UICollectionView works well with a some cool effects. But do you really need to add 5 delegate methods and a layout system for 5 labels that will never move?

这带来了另一个缺陷。它们还用于永远不会改变的简短、恒定的列表。在这种情况下,只需制作一个 Xib。或者编写一个自定义视图来堆叠它们。为什么?因为您不需要通过按钮或开关来管理 5 组标签。如果它们可能会改变,那么是的,使用列表。如果你想要物理,那么 UICollectionView 可以很好地使用一些很酷的效果。但是你真的需要为 5 个永远不会移动的标签添加 5 个委托方法和一个布局系统吗?

Also, I'm not forgetting that iOS has a native stacking view now too. I can never get it to deform how I want, even though I'm quite adept at the 2D and animation systems, so I never use the built-in one.

另外,我没有忘记 iOS 现在也有一个原生的堆叠视图。我永远无法让它按照我想要的方式变形,即使我非常擅长 2D 和动画系统,所以我从不使用内置的。

All I'm saying is, define your requirements. Maybe you don't need either of these, if your UI isn't adding/removing items and refreshing itself. Or maybe you want to write a Card Game and throw them out virtually on a table, then use UICollectionView with a physics system for its layout guide.

我要说的是,定义您的要求。如果您的 UI 没有添加/删除项目和刷新自身,那么您可能不需要其中任何一个。或者,也许您想编写一个纸牌游戏并将它们虚拟地扔到桌子上,然后使用 UICollectionView 和物理系统作为其布局指南。