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的對象。swift
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. api
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. app
表格視圖是由零個及以上的部分(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.ide
表格視圖能夠有UITableViewStylePlain和UITableViewStyleGrouped兩種類型中的一個。當建立了一個UITableView實例的時候,必定要規定表格的類型,類型值是不能夠改變的。在plain(無格式)類型中,section header 和 footer 在內容之上漂浮,若是那部分的完成的section是可視的。表格視圖能夠有以一個在視圖右手邊以條狀顯示的索引(好比A到Z 通信錄裏面的那種)。你能夠點觸一個label跳轉到目標section。grouped(分組)型的表格視圖爲全部cells提供了一個默認的背景顏色和一個默認的背景視圖。背景視圖提供了一個對特定的section全部cell可視的分組。好比說,一個分組能夠是一我的的名字和頭銜,另外一個分組能夠是那我的的電話號碼,再一個分組能夠是郵件帳號等等。查看分組表格的設置應用例子。表格應用分組類型中不能夠有索引。ui
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調用就不會觸發一次從新載入。
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
-
Initializes and returns a table view object having the given frame and 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 or nil
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
method initWithFrame:
, the UITableViewStylePlain
style is used as a default.
Import Statement
import UIKit
Availability
Available in iOS 2.0 and later.