以下依功能分類整理 UITableView 的絕大部分常用方法。
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
建立表視圖,指定區域與樣式。
- (NSInteger)numberOfRowsInSection:(NSInteger)section // 回傳某分組中的列數
- (NSInteger)numberOfSections // 回傳分組數
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
// 註冊含指定標識的表單元 nib 物件
- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier
// 註冊含頁眉或頁腳的 nib 物件
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
// 註冊用於建立表單元的類別
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier
// 註冊用於建立頁眉或頁腳的類別
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
// 回傳指定標識的可重用表單元
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath
// 回傳指定路徑上、指定標識的可重用表單元
- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
// 回傳指定標識的可重用頁眉/頁腳視圖
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
// 回傳指定分區的頁眉視圖
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section
// 回傳指定分區的頁腳視圖
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated
// 滾動至指定路徑的列,並對齊到螢幕上的指定位置
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated
// 滾動到離選定列最近的位置
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath // 指定位置的表單元
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell // 某表單元所在的位置
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point // 給定點所在列的位置
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect // 給定區域內各列的位置
- (NSArray *)visibleCells // 可見的表單元
- (NSArray *)indexPathsForVisibleRows // 所有可見列的索引
- (NSIndexPath *)indexPathForSelectedRow // 選取列的索引
- (NSArray *)indexPathsForSelectedRows // 所有選取列的索引
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UITableViewScrollPosition)scrollPosition
// 選取指定路徑的列
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
// 取消選取指定路徑的列
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
// 依 indexPath 陣列插入列
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
// 依 indexPath 陣列刪除列
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
// 把一列從一處移到另一處
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
// 插入分區
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
// 刪除分區
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
// 移動分區
- (void)reloadData
// 重載整個表的所有資料
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
// 只重載部分列,不必刷新整個表
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
// 重載指定分區
- (void)reloadSectionIndexTitles
// 重載右側索引欄的資料
實務上有三個地方最容易出錯。第一,任何插入/刪除/移動操作都必須成對更新底層資料來源,否則下一次 reloadData 就會因為列數不符而 crash。第二, performBatchUpdates: 包住的多個操作會合併成一次動畫,比連續呼叫數次流暢得多。第三,cellForRowAtIndexPath: 是效能熱點,應避免在其中做重運算或建立物件;搭配 dequeueReusableCellWithIdentifier:forIndexPath: 與 cell 重用,才能應付長列表的滾動。
本文整理自官方文件的閱讀筆記,若有任何翻譯或理解上的偏差,歡迎指正。