UITableView Overview

A table view displays a list of items in a single column.UITableViewis a subclass ofUIScrollView, which allows users to scroll through the table, althoughUITableViewallows vertical scrolling only. The cells comprising the individual items of the table areUITableViewCellobjects;UITableViewuses 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.

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.

Table views can have one of two styles,plainandgrouped. When you create aUITableViewinstance 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.

Many methods ofUITableViewtakeNSIndexPathobjects as parameters and return values.UITableViewdeclares a category onNSIndexPaththat enables you to get the represented row index (rowproperty) and section index (sectionproperty), and to construct an index path from a given row index and section index (init(row:section:)method). Especially in table views with multiple sections, you must evaluate the section index before identifying a row by its index number.

AUITableViewobject 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 customUITableViewControllerobject. The data source must adopt theUITableViewDataSourceprotocol and the delegate must adopt theUITableViewDelegateprotocol. The data source provides information thatUITableViewneeds 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.

When sent asetEditing(_:animated:)message (with a first parameter oftrue), the table view enters into editing mode where it shows the editing or reordering controls of each visible row, depending on theeditingStyleof each associatedUITableViewCell. Clicking on the insertion or deletion control causes the data source to receive atableView(_:commit:forRowAt:)message. You commit a deletion or insertion by callingdeleteRows(at:with:)orinsertRows(at:with:), as appropriate. Also in editing mode, if a table-view cell has itsshowsReorderControlproperty set totrue, the data source receives atableView(_:moveRowAt:to:)message. The data source can selectively remove the reordering control for cells by implementingtableView(_:canMoveRowAt:).

UITableViewcaches table-view cells for visible rows. You can create customUITableViewCellobjects with content or behavioral characteristics that are different than the default cells;A Closer Look at Table View Cellsexplains how.

UITableViewoverrides thelayoutSubviews()method ofUIViewso that it callsreloadData()only when you create a new instance ofUITableViewor when you assign a new data source. Reloading the table view clears current state, including the current selection. However, if you explicitly callreloadData(), it clears this state and any subsequent direct or indirect call tolayoutSubviews()does not trigger a reload.

For information about basic view behaviors, seeView Programming Guide for iOS.

State Preservation

In iOS 6 and later, if you assign a value to a table view’srestorationIdentifierproperty, it attempts to preserve the currently selected rows and the first visible row. The table’s data source may adopt theUIDataSourceModelAssociationprotocol, 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 theUIDataSourceModelAssociationprotocol, 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 theUIDataSourceModelAssociationprotocol, the scroll position will be saved and restored directly, as will the index paths for selected cells.

For more information about how state preservation and restoration works, seeApp Programming Guide for iOS.

For more information about appearance and behavior configuration, seeTable Views.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末皂冰,一起剝皮案震驚了整個(gè)濱河市身笤,隨后出現(xiàn)的幾起案子俊犯,更是在濱河造成了極大的恐慌神汹,老刑警劉巖赦肃,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件适室,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡馏艾,警方通過查閱死者的電腦和手機(jī)劳曹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來琅摩,“玉大人铁孵,你說我怎么就攤上這事》孔剩” “怎么了蜕劝?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長志膀。 經(jīng)常有香客問我,道長鳖擒,這世上最難降的妖魔是什么溉浙? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮蒋荚,結(jié)果婚禮上戳稽,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好惊奇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布互躬。 她就那樣靜靜地躺著,像睡著了一般颂郎。 火紅的嫁衣襯著肌膚如雪吼渡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天乓序,我揣著相機(jī)與錄音寺酪,去河邊找鬼。 笑死替劈,一個(gè)胖子當(dāng)著我的面吹牛寄雀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播陨献,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼盒犹,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了眨业?” 一聲冷哼從身側(cè)響起急膀,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎坛猪,沒想到半個(gè)月后脖阵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡墅茉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年命黔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片就斤。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡悍募,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出洋机,到底是詐尸還是另有隱情坠宴,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布绷旗,位于F島的核電站喜鼓,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏衔肢。R本人自食惡果不足惜庄岖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望角骤。 院中可真熱鬧隅忿,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至链峭,卻和暖如春畦娄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背熏版。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來泰國打工纷责, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人撼短。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓再膳,卻偏偏與公主長得像,于是被迫代替她去往敵國和親曲横。 傳聞我的和親對(duì)象是個(gè)殘疾皇子喂柒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容