Overview of the Table View API
表格视图API概述
The table view programming interface includes several UIKit classes, two formal protocols, and a category added to a Foundation framework class.
表格视图编程接口包括好几个UIKit类,两个正式的协议以及添加到Foundation 框架类的一个类别(category)。
Table View
一、表格视图
A table view itself is an instance of the UITableView
class. You use its methods to configure the appearance of the table view—for example, specifying the default height of rows or providing a subview used as the header for the table. Other methods give you access to the currently selected row as well as specific rows or cells. You can call other methods of UITableView
to manage selections, scroll the table view, and insert or delete rows and sections.
表格视图是UITableView类的一个实例。你使用它的方法来配置表格视图的外形--比如,指定行的默认高度或提供一个子视图作为表格的表头。其它方法让你访问当前被选中的行以及特定行或单元格。你可以调用UITableView类的其它方法来管理选择(selections),管理表格视图,以及插入或删除行和区(sections)。
UITableView
inherits from the UIScrollView
class, which defines scrolling behavior for views with content larger than the size of the window. UITableView
redefines the scrolling behavior to allow vertical scrolling only.
UITableView是UIScrollView类的子类,滚动视图定义了视图的滚动行为,当视图内容比窗口尺寸大时,可以滚动视图。UITableView 重新定义了滚动行为,它让视图只能纵向滚动。
Table View Controller
二、表格视图控制器
The UITableViewController
class manages a table view and adds support for many standard table-related behaviors such as selection management, row editing, table configuration, and others. This additional support is there to minimize the amount of code you have to write to create and initialize your table-based interface. You don’t use this class directly—instead you subclass UITableViewController
to add custom behaviors.
UITableViewController 类管理一个表格视图以及添加支持了很多跟表格相关的标准行为,比如选择管理,行编辑,表格配置,等等。 这种额外的支持减少了你必须编写用来创建和初始化基于表格的接口的代码总量。你不会直接使用该类---相反,你需要子类化UITableViewController 来添加自定义行为。
Data Source and Delegate
三、数据源和委托
A UITableView
object must have a delegate and a data source. Following the Model-View-Controller design pattern, the data source mediates between the app’s data model (that is, its model objects) and the table view. The delegate, on the other hand, manages the appearance and behavior of the table view. The data source and the delegate are often (but not necessarily) the same object, and that object is usually a custom subclass of UITableViewController
. (See“Navigating a Data Hierarchy with Table Views” for further information.)
UITableView对象必须有一个委托和一个数据源。 遵循模型-视图-控制器 设计模式,数据源就是应用程序数据模型(模型对象)和表格视图之间的媒介。 另一方面,委托管理表格视图的外形和行为。数据源和委托常常(但不一定)是同一个对象,并且该对象通常是UITableViewController 的一个子类。 (参见“Navigating a Data Hierarchy with Table Views” 了解进一步的信息.)
The data source adopts the UITableViewDataSource
protocol. UITableViewDataSource
has two required methods. The tableView:numberOfRowsInSection:
method tells the table view how many rows to display in each section, and the tableView:cellForRowAtIndexPath:
method provides the cell to display for each row in the table. Optional methods allow the data source to configure multiple sections, provide headers and/or footers, and support adding, removing, and reordering rows in the table.
数据源采用了UITableViewDataSource 协议。UITableViewDataSource 有两个必须的方法。方法 tableView:numberOfRowsInSection: 告诉表格视图每个区里有多少行,
以及方法 tableView:cellForRowAtIndexPath: 提供了表格中每行显示的单元格。可选方法让数据源配置多个区,提供表头和/或表尾,以及支持添加,删除,和在表格中重新排序行。
The delegate adopts the UITableViewDelegate
protocol. This protocol has no required methods. It declares methods that allow the delegate to modify visible aspects of the table view, manage selections, support an accessory view, and support editing of individual rows in a table.
委托采用了 UITableViewDelegate 协议。该协议没有必须方法。它声明了允许委托修改表格视图可见内容的各种方法,管理选择项,支持一个辅助视图,以及在表中支持编辑单独行。
An app can make use of the convenience class UILocalizedIndexedCollation
to help the data source organize the data for indexed lists and display the proper section when users tap an item in the index. TheUILocalizedIndexedCollation
class also localizes section titles.
应用程序可以利用方便类UILocalizedIndexedCollation 来帮助数据源为索引列表组织数据,并在当用户点击索引里的一个数据项时显示正确的区。
Extension to the NSIndexPath Class
四、扩展NSIndexPath类
Many table view methods use index paths as parameters or return values. An index path identifies a path to a specific node in a tree of nested arrays, and in the Foundation framework it is represented by an NSIndexPath
object. UIKit declares a category on NSIndexPath
with methods that return key paths, locate rows in sections, and constructNSIndexPath
objects from row and section indexes. For more information, see NSIndexPath UIKit Additions.
很多表格视图使用索引路径作为参数或返回值。索引路径在嵌套数组树中标识一个路径到一个特定的节点,在Foundation 框架中它由一个NSIndexPath 对象表示。UIKit为NSIndexPath声明了一个类别,以及返回关键路径,在区中定位行,以及用行和区索引结构建NSIndexPath 对象的各种方法,更多信息,请看NSIndexPath UIKit Additions.
Table View Cells
五、表格视图单元格
As noted in “Data Source and Delegate,” the data source must return a cell object for each visible row that a table view displays. These cell objects must inherit from the UITableViewCell
class. This class includes methods for managing cell selection and editing, managing accessory views, and configuring the cell. You can instantiate cells directly in the standard styles defined by the UITableViewCell
class and give these cells content consisting of one or two strings of text and, in some styles, both image and text. Instead of using a cell in a standard style, you can put your own custom subviews in the content view of an “off-the-shelf” cell object. You may also subclass UITableViewCell
to customize the appearance and behavior of table view cells. These approaches are all discussed in “A Closer Look at Table View Cells.”
正如“Data Source and Delegate,” 里所讲, 数据源必须为表格视图显示的每个可见行返回一个单元格对象。这些单元格对象必须继承自UITableViewCell 类。 该类包含了管理单元格选项以及编辑,管理辅助视图,和配置单元格的各种方法。你可以从UITableViewCell 类定义的标准风格中直接实例化单元格,然后给这些单元格提供由一个或两个字符串文件组成的内容,或者一些风格中,由图片和文本组成。你可以在一个现成的单元格对象的内容视图里放置你自己定义子视图,而不使用一个标准风格的单元格。你也可以子类化UITableViewCell类来自定义表格视图单元格的外形和行为。这些实现都在“A Closer Look at Table View Cells.” 里有讨论。