Collection View Programming Guide for iOS---(一)----About iOS Collection Views

时间:2023-03-08 16:50:56
Collection View Programming Guide for iOS---(一)----About iOS Collection Views

About iOS Collection Views

关于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 view 是运用一个灵活多变的布局呈现一系列有序数据项的一种方法。collection view最通常的使用使用像网格状排列来呈现数据项,但是iOS的collection view 的能力不仅限于行和列。使用collection views, 视觉元素的精确布局可通过子类化定义并被动态改变。所以你可以实现网格,栈,圆形布局,动态改变布局,或任何你可以想象的排列布局。

Collection views keep a strict separation between the data being presented and the visual elements used to present that data. 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, whose job is to specify the placement and visual attributes for your views. 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 views 严格分离被呈现的数据和用来呈现这些数据的视觉元素。应用程序只负责管理这些数据,以及提供用来呈现这些数据的视图对象。然后collection view接手这些视图,并完成所有的工作把数据定位在屏幕上。它和一个层对象(layout object)联合工作,该层对象的工作是指定视图的位置和可视化属性。因此,你提供数据,层对象提供位置信息,collection view 把这两部分融合在一起来达到最后的外观。

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 类提供了实现简单网格的所有行为(behavior)。你也可以扩展标准类来支持自定义布局,来实现跟那些布局的特殊交互。

A Collection View Manages the Visual Presentation of Data Driven Views

Collection View 管理驱动视图的可视化数据呈现

A collection view facilitates the presentation of data-driven views provided by your app. The collection view’s only concern is about 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, the layout object, and your custom objects is crucial for using collection views in your app.

collection view 促进应用程序提供的数据驱动(data-driven)视图的呈现。collection view 的唯一关注点是接手你的视图并以特定的方式把它们布局到屏幕上。collection view 关注的是视图的呈现和排列,而不是关于它们的内容。想要在应用里使用collection view, 理解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提供的一个具体布局对象。 通常你使用它来实现网格---就是,数据项的行和列---但是流布局还支持任何类型的线性流。因为它不仅限于网格,你可以使用它来为你的内容创建各种有趣复杂的排列,既可以子类化它也可以直接使用。 流布局支持不同尺寸的数据项, 项的间距可变,可以自定义页头(header)和页脚(footer), 已经无需子类化就可以定制页边空白(margin)。 子类化能让你更进一步的调整流布局类的行为。

Relevant chapter: “Using the Flow Layout”

相关章节:“Using the Flow Layout”

Gesture Recognizers Can Be Used for Cell and Layout Manipulations

手势识别能用于单元(cell)和布局操作

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 the collection view.

就像所有视图一样,你可以在collection view里采用手势识别来操作视图的内容。因为collection view 涉及了多个视图的协作, 在collection view 里结合手势识别能有助于理解一些基本技术。你可以在collection view里调整(tweak)层属性或操作数据项。

Custom Layouts Let You Go Beyond Grids

自定义布局让你超越网格布局

The basic layout object can be subclassed to implement custom layouts for your app. Designing a custom layout does not require a large amount of code in most cases. However, it helps to understand how layouts work so that you can design your layout objects to be efficient.

基本的布局对象能被子类化来实现应用程序里的自定义布局。 在大多数情况下,自定义布局不需要大量的的代码。 然而,它能帮助你理解布局是如何工作的,那样你就能设计高效的布局对象。

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应用中的视图有一个坚实的理解。如果你是iOS编程的新手,不熟悉iOS视图架构,请先阅读View Programming Guide for iOS

See Also(同时查看)

For a guided overview of collection views, see the following WWDC videos:

collection views的指导性概述,请看以下WWDC视频:

Collection views are somewhat related to table views, in that both present ordered data to the user. 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)有一些关联, 两者都是给用户呈现有序数据。然而,表格视图的视觉呈现是面向单列布局,而collection views 能支持很多不同的布局。 关于表格视图的更多信息,请看Table View Programming Guide for iOS.