一开伏、補充
在上一篇簡書中遭商,http://www.reibang.com/p/1a3533d8a258劫流,我將IOS中tableView(表視圖)的一些常用方法總結(jié)了一下,這篇將tableView的代理方法作了總結(jié)仍秤,對上一篇簡書進行了補充可很。
二、UITableViewDataSourc(數(shù)據(jù)源代理)
1苇本、必須實現(xiàn)的回調(diào)方法
返回每個分區(qū)的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
返回每一行的cell
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath;
2菜拓、可選實現(xiàn)的方法
返回分區(qū)數(shù)(默認為1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
返回每個分區(qū)頭部的標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
返回每個分區(qū)的尾部標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
設(shè)置某行是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
設(shè)置某行是否可以被移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
設(shè)置索引欄標(biāo)題數(shù)組(實現(xiàn)這個方法纳鼎,會在tableView右邊顯示每個分區(qū)的索引)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
設(shè)置索引欄標(biāo)題對應(yīng)的分區(qū)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
tableView接受編輯時調(diào)用的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
這個方法中的editingStyle參數(shù)是一個枚舉喷橙,代表了cell被編輯的模式,如下:
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,//沒有編輯操作
UITableViewCellEditingStyleDelete,//刪除操作
UITableViewCellEditingStyleInsert//插入操作
};
tableView的cell被移動時調(diào)用的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
三悬荣、UITableViewDelegate(tableView代理)
cell將要顯示時調(diào)用的方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
頭視圖將要顯示時調(diào)用的方法
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section;
尾視圖將要顯示時調(diào)用的方法
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section;
和上面的方法對應(yīng)氯迂,這三個方法分別是cell嚼蚀,頭視圖,尾視圖已經(jīng)顯示時調(diào)用的方法
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath;
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section;
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section;
設(shè)置行高弄捕,頭視圖高度和尾視圖高度的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
設(shè)置行高导帝,頭視圖高度和尾視圖高度的估計值(對于高度可變的情況下您单,提高效率)
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;
設(shè)置自定義頭視圖和尾視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
設(shè)置cell是否可以高亮
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath;
cell高亮和取消高亮?xí)r分別調(diào)用的函數(shù)
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath;
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath;
當(dāng)即將選中某行和取消選中某行時調(diào)用的函數(shù)虐秦,返回一直位置,執(zhí)行選中或者取消選中
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
已經(jīng)選中和已經(jīng)取消選中后調(diào)用的函數(shù)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath;
設(shè)置tableView被編輯時的狀態(tài)風(fēng)格蜈彼,如果不設(shè)置俺驶,默認都是刪除風(fēng)格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
自定義刪除按鈕的標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
設(shè)置編輯時背景是否縮進
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
將要編輯和結(jié)束編輯時調(diào)用的方法
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath;
移動特定的某行
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:
(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;