An instance of UITableView
(or simply, a table view) is a means for displaying and editing hierarchical lists of information.
一个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是UIScrollView的一个子类,它允许用户滚动表格,但是UITableView只允许垂直方向上的滚动。由独立项组成的单元格是UITableViewCell的对象。
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使用这些对象来拖动表格的可视行。单元格是有内容的-标题和图片-也可以有,在右边缘,副视图(就是那种disclosure点击去跳到另一个视图中去)。标准的副视图是展开指示器或者细节展开按钮;前者导向到数据层级结构中的下一级,后者导向到所选项的一个细节视图。副视图也可以是framework controls(框架控制),比如switch(开关)和slider(滑动条),或者也可以是自定义视图。表格视图可以进入一个编辑模式,在该模式下用户可以插入,删除和对表格的行进行排序。
A table view is made up of zero or more sections, each with its own rows. Sections are identified by their index number within the table view, and rows are identified by their index number within a section. Any section can optionally be preceded by a section header, and optionally be followed by a section footer.
表格视图是由零个及以上的部分(section)组成,每个section都有它自己的行。Sections是由它们的表格视图内的索引数字标记的,Row(行)是由它们的Section内的索引数字标记的。任何Section前面可以选择性地有section header(头部),也选择性地有section footer(足部)。
Table views can have one of two styles, UITableViewStylePlain
and UITableViewStyleGrouped
. When you create a UITableView
instance you must specify a table style, and this style cannot be changed. In the plain style, section headers and footers float above the content if the part of a complete section is visible. A table view can have an index that appears as a bar on the right hand side of the table (for example, "A" through "Z"). You can touch a particular label to jump to the target section. The grouped style of table view provides a default background color and a default background view for all cells. The background view provides a visual grouping for all cells in a particular section. For example, one group could be a person‘s name and title, another group for phone numbers that the person uses, and another group for email accounts and so on. See the Settings application for examples of grouped tables. Table views in the grouped style cannot have an index.
表格视图可以有UITableViewStylePlain和UITableViewStyleGrouped两种类型中的一个。当创建了一个UITableView实例的时候,一定要规定表格的类型,类型值是不可以改变的。在plain(无格式)类型中,section header 和 footer 在内容之上漂浮,如果那部分的完成的section是可视的。表格视图可以有以一个在视图右手边以条状显示的索引(比如A到Z 通讯录里面的那种)。你可以点触一个label跳转到目标section。grouped(分组)型的表格视图为所有cells提供了一个默认的背景颜色和一个默认的背景视图。背景视图提供了一个对特定的section所有cell可视的分组。比如说,一个分组可以是一个人的名字和头衔,另一个分组可以是那个人的电话号码,再一个分组可以是邮件账号等等。查看分组表格的设置应用例子。表格应用分组类型中不可以有索引。
Many methods of UITableView
take NSIndexPath
objects as parameters and return values. UITableView
declares a category on NSIndexPath
that enables you to get the represented row index (row
property) and section index (section
property), and to construct an index path from a given row index and section index (indexPathForRow:inSection:
method). Especially in table views with multiple sections, you must evaluate the section index before identifying a row by its index number.
UITableView的许多方法使用NSIndexPath对象作为参数和返回值。UITableView在NSIndexPath中声明了一个类(category),来授权你获得显示行索引(row属性)和section索引(section属性),从一个给予的row索引和section索引(indexPathForRow:inSection:方法)构造一个索引路径。尤其是在有多个section的表格视图,一定要在识别一个行row之前校验section索引值。
A UITableView
object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController
object. The data source must adopt the UITableViewDataSource
protocol and the delegate must adopt the UITableViewDelegate
protocol. The data source provides information that UITableView
needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered. The delegate manages table row configuration and selection, row reordering, highlighting, accessory views, and editing operations.
UITableView对象一定有一个作为数据源的对象和一个座位delegate(委托、代表)的对象;典型的是这些对象是应用代表或者,更常见的,一个自定义的UITableViewController对象。数据源一定要采用UITableViewDataSource协议,delegate一定要采用UITableViewDelegate协议。数据源提供了当一个表格被插入、删除或者重新排序时UITableView需要用来构造表格和管理数据的模型。delegate管理表格行的配置和选择,行重排序,强调,副视图和编辑操作。
When sent a setEditing:animated:
message (with a first parameter of YES
), the table view enters into editing mode where it shows the editing or reordering controls of each visible row, depending on the editingStyle
of each associated UITableViewCell
. Clicking on the insertion or deletion control causes the data source to receive a tableView:commitEditingStyle:forRowAtIndexPath:
message. You commit a deletion or insertion by calling deleteRowsAtIndexPaths:withRowAnimation:
or insertRowsAtIndexPaths:withRowAnimation:
, as appropriate. Also in editing mode, if a table-view cell has its showsReorderControl
property set to YES
, the data source receives a tableView:moveRowAtIndexPath:toIndexPath:
message. The data source can selectively remove the reordering control for cells by implementing tableView:canMoveRowAtIndexPath:
.
当发送一个setEditing:animated:信息(第一个参数是YES的情况下),表格视图进入编辑模式,在该模式中显示对每个可视行的编辑或者重新排序操作,这取决于分配给每个UITableViewCell的editingStyle(编辑形式)值,点击插入或者删除控制会使数据源接受一个tableView:commitEditingStyle:forRowAtIndexPath:信息。你可以适时通过调用deleteRowsAtIndexPaths:withRowAnimation:或者 insertRowsAtIndexPaths:withRowAnimation:提交删除或者插入请求。同样地,在编辑模式中,如果一个表格视图的单元格的showsReorderControl属性被设为YES,数据源接受一个tableView:moveRowAtIndexPath:toIdnexPath:信息。数据源通过实现tableView:canMoveRowAtIndexPath:方法对单元格可以选择移除或者重新排序操作
UITableView
caches table-view cells for visible rows. You can create custom UITableViewCell
objects with content or behavioral characteristics that are different than the default cells; "??A Closer Look at Table View Cells in Table View Programming Guide for iOS" in Table View Programming Guide for iOS explains how.
UITableView为可视的行缓存表格视图的单元格。可以用内容(content)或者行为特征创建与默认的单元格不同的自定义的UITableViewCell对象;Table View Programming Guide for iOSiOS表格视图编程指导中的"??A Closer Look at Table View Cells in Table View Programming Guide for iOS"解释了如何去操作。
UITableView
overrides the layoutSubviews
method of UIView
so that it calls reloadData
only when you create a new instance of UITableView
or when you assign a new data source. Reloading the table view clears current state, including the current selection. However, if you explicitly call reloadData
, it clears this state and any subsequent direct or indirect call to layoutSubviews
does not trigger a reload.
UITableView 重载了UIView的layoutSubviews方法所以它仅调用reloadData(重载数据)当你创建了一个新的UITableView实例活着当你分配了新的数据源。重载表格视图清除了当前的状态,包括当前的选择。但是,如果明确地调用了reloadData,它清除了这个状态和任何其他随后的直接或者间接对layoutSubviews调用就不会触发一次重新载入。
For information about basic view behaviors, see View Programming Guide for iOS.
State Preservation
In iOS 6 and later, if you assign a value to a table view’s restorationIdentifier
property, it attempts to preserve the currently selected rows and the first visible row. The table’s data source may adopt the UIDataSourceModelAssociation
protocol, which provides a way to identify a row’s contents independent of that row’s position in the table. If the table’s data source adopts the UIDataSourceModelAssociation
protocol, the data source will be consulted when saving state to convert the index paths for the top visible row and any selected cells to identifiers. During restoration, the data source will be consulted to convert those identifiers back to index paths and reestablish the top visible row, and reselect the cells. If the table’s data source does not implement the UIDataSourceModelAssociation
protocol, the scroll position will be saved and restored directly, as will the index paths for selected cells.
在iOS6或之后,如果你给表格视图的restorationIndentifier属性分配了一个值的话,它会尝试去保护当前选中的行和视图的第一可视行。表格的数据源可能采用UIDataSourceModelAssociation协议,它提供了一种去识别一个行的内容之于行在这个表格中的位置的独立性的方法。如果表格的数据源采用UIDataSourceModelAssociation协议,当保存状态来转换顶部可视行和其他任何单元格的索引路径为标签时,数据源将会被询问。在恢复期间,数据源将会被询问来转换这些标签回索引路径然后重新建立顶部可视行,再重新选择单元格。如果表格的数据源没有实现UIDataSourceModelAssocaition协议,滚动位置将会被保存和直接恢复为所选单元格的索引路径。
For more information about how state preservation and restoration works, see App Programming Guide for iOS.
For more information about appearance and behavior configuration, see Table Views in UIKit User Interface Catalog.
Initializing a UITableView Object
initWithFrame:style:
Initializes and returns a table view object having the given frame and style.
Declaration
SWIFT
init(frame
frame
: CGRect,
stylestyle
: UITableViewStyle)OBJECTIVE-C
- (instancetype)initWithFrame:(CGRect)
frame
style:(UITableViewStyle)style
Parameters
frame
A rectangle specifying the initial location and size of the table view in its superview‘??s coordinates. The frame of the table view changes as table cells are added and deleted.
style
A constant that specifies the style of the table view. See Table View Style for descriptions of valid constants.
Return Value
Returns an initialized
UITableView
object ornil
if the object could not be successfully initialized.Discussion
You must specify the style of a table view when you create it and you cannot thereafter modify the style. If you initialize the table view with the
UIView
methodinitWithFrame:
, theUITableViewStylePlain
style is used as a default.Import Statement
import UIKit
Availability
Available in iOS 2.0 and later.