About iOS Collection Views
A collection view is a way to present an ordered set of data items using a flexible and changeable layout. The most common use for collection views is to present items in a grid-like arrangement, but collection views in iOS are capable of more than just rows and columns. With collection views, the precise layout of visual elements is definable through subclassing and can be changed dynamically, so you can implement grids, stacks, circular layouts, dynamically changing layouts, or any type of arrangement you can imagine.
Collection views keep a strict separation between the data being presented and the visual elements used to present that data. In most cases, your app is solely responsible for managing the data. Your app also provides the view objects used to present that data. After that, the collection view takes your views and does all the work of positioning them onscreen. It does this work in conjunction with a layout object, which specifies the placement and visual attributes for your views and that can be subclassed to fit your app’s exact needs. Thus, you provide the data, the layout object provides the placement information, and the collection view merges the two pieces together to achieve the final appearance
collection view
是一种使用灵活、可变的布局来呈现有序的数据的方式。collection view
最常见的用途就是用来展示网格状排布的items,但iOS中collection view
不仅仅局限在展示行和列这样的数据。通过collection view
,可视化元素的精确布局是通过子类化来定义的,并且是可以被动态改变的,因此你可以实现网格、堆栈,圆形、动态变化布局,或者其他任何你可以想象到的排布类型。
collection view
在正在呈现的数据(model)和使用数据的可视元素(view)之间有严格的分离(区分)。在大部分情况下,你的APP仅仅负责管理数据。您的APP也需要提供视图对象。除此之外,collection view
拿着你的views并做完所有需要在屏幕上展示的工作。它做这些工作是结合了一个layout对象,该对象指定位置和view的可视属性,并且可以通过子类化来适合你APP的具体需求。因此,你提供数据、layout对象(指定布局信息),然后collection view
合并这2个部分来实现最终的显示
At a Glance (概览)
The standard iOS collection view classes provide all of the behavior you need to implement simple grids. You can also extend the standard classes to support custom layouts and specific interactions with those layouts.
标准的iOS collection view类提供了实现简单网格所需的所有行为。你也可以扩展标准的类来支持自定义layout,并指明和其他layout的交互
A Collection View Manages the Visual Presentation of Data-Driven Views
A collection view facilitates the presentation of data-driven views provided by your app. The collection view’s only concern is taking your views and laying them out in a specific way. The collection view is all about the presentation and arrangement of your views and not about their content. Understanding the interactions between the collection view, its data source, the layout object, and your custom objects is crucial for using collection views in your app, particularly in smart and efficient ways.
collection view
便于呈现数据驱动的views。collection view
唯一关心的是views和布局他们的特定方式。collection view
就是用来呈现和安排views,但不包括views的内容。理解collection view
、它的data source、它的layout、和你自定义的objects的相互配合,在使用collection view时是至关重要的
The Flow Layout Supports Grids and Other Line-Oriented Presentations
A flow layout object is a concrete layout object provided by UIKit. You typically use the flow layout object to implement grids—that is, rows and columns of items—but the flow layout supports any type of linear flow. Because it is not just for grids, you can use the flow layout to create interesting and flexible arrangements of your content both with and without subclassing. The flow layout supports items of different sizes, variable spacing of items, custom headers and footers, and custom margins without subclassing. And subclassing allows you to tweak the behavior of the flow layout class even further.
flow layout是UIKit框架提供的具体的布局对象(layout)。你通常使用flow layout来实现网格(行列的items)-支持任何类型的线性流。因为它不仅仅是用来实现网格,你可以使用flow layout来有趣和灵活的创建你的内容(可以子类化也可以不子类化)。flow layout可以在不用子类化的情况下 支持不同尺寸,可变间距(spcing),自定义header和footer和自定义margin。子类化允许你进一步调整flow layout的行为
Gesture Recognizers Can Be Used for Cell and Layout Manipulations
Like all views, you can attach gesture recognizers to a collection view to manipulate the content of that view. Because a collection view involves the collaboration of multiple views, it helps to understand some basic techniques for incorporating gesture recognizers into your collection views. You can use gesture recognizers to tweak layout attributes or to manipulate items in a collection view.
就像所有的视图(views)一样,你可以附加手势识别器到collection view
上来实现视图(view)内容的控制。因为collection view
包含(牵涉)多个视图的协作,它有助于理解一些基本的collection views上包含手势识别器的技术。你可以使用手势来调整layout attributes或者控制items
Custom Layouts Let You Go Beyond Grids
You can subclass the basic layout object to implement custom layouts for your app. Even though designing a custom layout does not typically require a large amount of code, the more you understand how layouts work, the better you can design your layout objects to be efficient. The guide’s final chapter focuses on an example project with a full implementation of a custom layout.
你可以继承基本的layout来实现自定义布局。尽管设计一个自定义layout通常不需要大量的代码,但你越了解布局的工作方式,你就可以更好的,更有效率的设计你的layout来。这份guide(指南)的最后的章节将聚焦于一个完全实现的自定义layout示例项目
Prerequisites (前提,首要条件)
Before reading this document, you should have a solid understanding of the role views play in iOS apps. If you are new to iOS programming and not familiar with the iOS view architecture, read View Programming Guide for iOS before reading this book.
在阅读这份文档之前,你应该对iOS中views所扮演的角色有一个基本的了解。如果你还是一个新人,并且不熟悉iOS中的view结构,请在阅读这份指南前阅读《View Programming Guide for iOS》
See Also
Collection views are somewhat related to table views, in that both present ordered data to the user. The implementation of a table view is similar to that of a standard collection view (one which uses the provided flow layout) in its use of index paths, cells, and view recycling. However, the visual presentation of table views is geared around a single-column layout, whereas collection views can support many different layouts. For more information about table views, see Table View Programming Guide for iOS.
Collection views
是和table views
有一些相关性,因为2者都将有序的数据呈现给用户。table view
的实现是和标准的collection view
(使用自带的flow layout)非常相似的,使用index paths(索引),cells,和view recycling(重用)。但是table views
的呈现是围绕单行(single-column)layout,而collection views
可以支持很多不同的布局。想了解更多table views,查看《Table View Programming Guide for iOS》
网友评论