UITableView回調和table相關成員方法詳解

1.UITableView的datasource實現:
//回調獲取每個section中的cell的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//回調獲取每個uitableviewcell,只有當需要顯示的cell在table的可視區域內才被回調
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//回調獲取table的section數量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//回調獲取table的每個section的header標題
- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
//回調獲取table的每個section的footer標題
- (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section
//回調判斷指定的cell是否能有編輯狀態
- (BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
//回調判斷指定的cell能否被移動當進入編輯模式的時候
- (BOOL)tableView:(UITableView )tableView canMoveRowAtIndexPath:(NSIndexPath )indexPath
//回調獲取table右邊的索引欄內容
- (NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
//當點擊右邊索引欄時執行的回調,可以根據點擊的title值返回應該跳到第幾個section
- (NSInteger)tableView:(UITableView )tableView sectionForSectionIndexTitle:(NSString )title atIndex:(NSInteger)index
//當所有相關編輯模式的回調,只實現該回調時默認能滑動cell出現刪除按鈕。當和其他相關的編輯模式回調混合使用分別有移動,插入,刪除等功能
- (void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
//當進入的是移動的編輯模式時,實現該回調進行移動cell和數據的更新
- (void)tableView:(UITableView )tableView moveRowAtIndexPath:(NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

2.UITableView的delegate實現:
//每個cell將要呈現時回調
- (void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath
//每個section的header將要呈現時回調
- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
//每個section的footer將要呈現時回調
- (void)tableView:(UITableView )tableView willDisplayFooterView:(UIView )view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
//每個cell呈現完畢後回調
- (void)tableView:(UITableView )tableView didEndDisplayingCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0)
//每個section的header呈現完畢後回調
- (void)tableView:(UITableView )tableView didEndDisplayingHeaderView:(UIView )view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
//每個section的footer呈現完畢後回調
- (void)tableView:(UITableView )tableView didEndDisplayingFooterView:(UIView )view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
//回調設置每行的高度,如果要自適應調整cell的高度,則必須要實現該回調,返回調整後的cell高度
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
//回調設置每個section的header高度,如果要自適應調整header的高度,則必須要實現該回調,返回調整後的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//回調設置每個section的footer高度,如果要自適應調整footer的高度,則必須要實現該回調,返回調整後的footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//回調設置每個section的header自定義view
- (UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section
//回調設置每個section的footer自定義view
- (UIView )tableView:(UITableView )tableView viewForFooterInSection:(NSInteger)section
//回調設置每行最右邊的輔助按鈕的樣式
/*
typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
UITableViewCellAccessoryNone, // don’t show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn’t track
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
UITableViewCellAccessoryCheckmark // checkmark. doesn’t track
};
*/
- (UITableViewCellAccessoryType)tableView:(UITableView )tableView accessoryTypeForRowWithIndexPath:(NSIndexPath )indexPath NS_DEPRECATED_IOS(2_0, 3_0)
//回調設置輔助按鈕被點擊後的事件
- (void)tableView:(UITableView )tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath )indexPath
//回調設置某行是否當被點擊後處於高亮狀態
- (BOOL)tableView:(UITableView )tableView shouldHighlightRowAtIndexPath:(NSIndexPath )indexPath NS_AVAILABLE_IOS(6_0)
//回調當某行處於高亮狀態時的行爲
- (void)tableView:(UITableView )tableView didHighlightRowAtIndexPath:(NSIndexPath )indexPath NS_AVAILABLE_IOS(6_0)
//回調當某行失去高亮狀態時的行爲
- (void)tableView:(UITableView )tableView didUnhighlightRowAtIndexPath:(NSIndexPath )indexPath NS_AVAILABLE_IOS(6_0)
//回調某行將要被選中的行爲
- (NSIndexPath )tableView:(UITableView )tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
//回調某行將要被取消選中的行爲
- (NSIndexPath )tableView:(UITableView )tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
//回調某行已經被選中點擊的行爲
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
//回調某行已經取消選中的行爲
- (void)tableView:(UITableView )tableView didDeselectRowAtIndexPath:(NSIndexPath )indexPath NS_AVAILABLE_IOS(3_0)
//回調設置某行進入了哪種編輯模式
- (UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
//回調設置某行進入刪除模式的刪除按鈕名字
- (NSString )tableView:(UITableView )tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
//回調設置進入編輯模式的行能否縮進
- (BOOL)tableView:(UITableView )tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath )indexPath
//回調將要進入編輯模式的行爲
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
//回調完成編輯模式的行爲
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
//回調設置某行的縮進級別
- (NSInteger)tableView:(UITableView )tableView indentationLevelForRowAtIndexPath:(NSIndexPath )indexPath
//回調設置某行被長按是否出現菜單欄
- (BOOL)tableView:(UITableView )tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath )indexPath NS_AVAILABLE_IOS(5_0)
//回調設置菜單欄是否顯示哪些菜單欄選項
- (BOOL)tableView:(UITableView )tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath )indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)
//回調點擊菜單欄選項觸發的事件
- (void)tableView:(UITableView )tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath )indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)

3.UITableView常用成員方法

//通過indexpath獲取指定行的uitableviewcell
[_table cellForRowAtIndexPath:indexPath];
//刪除某行並帶上動畫
[_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//刪除某個section並帶上動畫
[_table deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationMiddle];
//取消被選中的某行
[_table deselectRowAtIndexPath:indexPath animated:YES];
//返回某個section的header自定義view
[_table headerViewForSection:indexPath.row];
//返回某個section的footer自定義view
[_table footerViewForSection:indexPath.row];
//通過指定的cell獲取cell所在的行數
[_table indexPathForCell:cell];
//通過在table的位置座標返回該座標所在的cell的行數
[_table indexPathForRowAtPoint:CGPointMake(0, 0)];
//返回被選中的cell的行數
[_table indexPathForSelectedRow];
//返回table中一個範圍區域的cell的行數
[_table indexPathsForRowsInRect:CGRectMake(0, 0, 100, 100)];
//返回table視覺區域內的cell的行數
[_table indexPathsForVisibleRows];
//以動畫方式在指定的位置插入cell
[_table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//以動畫方式在指定位置插入section
[_table insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];
//返回指定位置的cell的rect屬性
[_table rectForRowAtIndexPath:indexPath];
//重新加載table,重新執行所有回調方法
[_table reloadData];
//指定重新加載table的某些行,並以動畫方式
[_table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
//重新加載索引欄
[_table reloadSectionIndexTitles];
//指定重新加載table的section,並以動畫方式
[_table reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
//滑動table到頂部
[_table scrollsToTop];
//帶動畫方式滑動table到指定區間
[_table scrollRectToVisible:CGRectMake(0, 0, 100, 100) animated:YES];
//帶動畫方式滑動table到指定位置附近
[_table scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES];
//以動畫方式從table的哪個指定位置滑動到指定的行
[_table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
//返回table可視區域內所有的cell
[_table visibleCells];
//beginupdates和endupdates要成對出現,對於同時操作多個insert,delete,selection的動畫操作需要把它們放在beginupdates和endupdates中間執行,這樣可以把多個動畫合成一個動畫來處理,實現原子性,降低出錯的機率
[_table beginUpdates];
[_table endUpdates];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章