tableView的delegate和dateSource 常用功能的列舉

零: 實(shí)例

1: 獲取tableviewCell在當(dāng)前屏幕中的坐標(biāo)(切換坐標(biāo))

CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];   
   
CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]]; 

2: 獲取當(dāng)前所顯示的cell的方法:

1.- (NSArray*)visibleCells;
UITableview的方法,這個(gè)最直接,返回一個(gè)UITableviewcell的數(shù)組。
對(duì)于自定制的cell,之后的處理可能稍微繁瑣些。

2.- (NSArray*)indexPathsForVisibleRows;
UITableview的又一個(gè)方法,這個(gè)比較好用了,返回一個(gè)NSIndexPath的數(shù)組,可以直接用indexpath.row去調(diào)你的table_related_Array里的數(shù)據(jù)了。比較方便用于自定制的cell。

3.- (CGRect)rectForRowAtIndexPath:(NSIndexPath*)indexPath;

一: UITableView的datasource代理方法:

  • 1: 回調(diào)獲取每個(gè)section中的cell的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  • 2: 回調(diào)獲取每個(gè)uitableviewcell,只有當(dāng)需要顯示的cell在table的可視區(qū)域內(nèi)才被回調(diào)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  • 3: 回調(diào)獲取table的section數(shù)量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  • 4: 回調(diào)獲取table的每個(gè)section的header標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  • 5: 回調(diào)獲取table的每個(gè)section的footer標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  • 6: 回調(diào)判斷指定的cell是否能有編輯狀態(tài)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  • 7: 回調(diào)判斷指定的cell能否被移動(dòng)當(dāng)進(jìn)入編輯模式的時(shí)候
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  • 8: 回調(diào)獲取table右邊的索引欄內(nèi)容
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
  • 9: 當(dāng)點(diǎn)擊右邊索引欄時(shí)執(zhí)行的回調(diào),可以根據(jù)點(diǎn)擊的title值返回應(yīng)該跳到第幾個(gè)section
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  • 10: 當(dāng)所有相關(guān)編輯模式的回調(diào),只實(shí)現(xiàn)該回調(diào)時(shí)默認(rèn)能滑動(dòng)cell出現(xiàn)刪除按鈕。當(dāng)和其他相關(guān)的編輯模式回調(diào)混合使用分別有移動(dòng),插入,刪除等功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  • 11: 當(dāng)進(jìn)入的是移動(dòng)的編輯模式時(shí),實(shí)現(xiàn)該回調(diào)進(jìn)行移動(dòng)cell和數(shù)據(jù)的更新
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

2.UITableView的delegate代理方法:

  • 1: 每個(gè)cell將要呈現(xiàn)時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  • 2: 每個(gè)section的header將要呈現(xiàn)時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 3: 每個(gè)section的footer將要呈現(xiàn)時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 4: 每個(gè)cell呈現(xiàn)完畢后回調(diào)
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0)
  • 5: 每個(gè)section的header呈現(xiàn)完畢后回調(diào)
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 6: 每個(gè)section的footer呈現(xiàn)完畢后回調(diào)
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 7: 回調(diào)設(shè)置每行的高度,如果要自適應(yīng)調(diào)整cell的高度,則必須要實(shí)現(xiàn)該回調(diào),返回調(diào)整后的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  • 8: 回調(diào)設(shè)置每個(gè)section的header高度,如果要自適應(yīng)調(diào)整header的高度,則必須要實(shí)現(xiàn)該回調(diào),返回調(diào)整后的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  • 9: 回調(diào)設(shè)置每個(gè)section的footer高度,如果要自適應(yīng)調(diào)整footer的高度,則必須要實(shí)現(xiàn)該回調(diào),返回調(diào)整后的footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  • 10: 回調(diào)設(shè)置每個(gè)section的header自定義view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  • 11: 回調(diào)設(shè)置每個(gè)section的footer自定義view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  • 12: 回調(diào)設(shè)置每行最右邊的輔助按鈕的樣式
/*
 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)
  • 13: 回調(diào)設(shè)置輔助按鈕被點(diǎn)擊后的事件
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  • 14: 回調(diào)設(shè)置某行是否當(dāng)被點(diǎn)擊后處于高亮狀態(tài)
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
  • 15: 回調(diào)當(dāng)某行處于高亮狀態(tài)時(shí)的行為
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
  • 16: 回調(diào)當(dāng)某行失去高亮狀態(tài)時(shí)的行為
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
  • 17: 回調(diào)某行將要被選中的行為
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
  • 18: 回調(diào)某行將要被取消選中的行為
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
  • 19: 回調(diào)某行已經(jīng)被選中點(diǎn)擊的行為
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  • 20: 回調(diào)某行已經(jīng)取消選中的行為
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
  • 21: 回調(diào)設(shè)置某行進(jìn)入了哪種編輯模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  • 22: 回調(diào)設(shè)置某行進(jìn)入刪除模式的刪除按鈕名字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
  • 23: 回調(diào)設(shè)置進(jìn)入編輯模式的行能否縮進(jìn)
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
  • 24: 回調(diào)將要進(jìn)入編輯模式的行為
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
  • 25: 回調(diào)完成編輯模式的行為
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
  • 26: 回調(diào)設(shè)置某行的縮進(jìn)級(jí)別
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
  • 27: 回調(diào)設(shè)置某行被長(zhǎng)按是否出現(xiàn)菜單欄
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0)
  • 28: 回調(diào)設(shè)置菜單欄是否顯示哪些菜單欄選項(xiàng)
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)
  • 29: 回調(diào)點(diǎn)擊菜單欄選項(xiàng)觸發(fā)的事件
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)

3.UITableView常用成員方法

  • 1: 通過(guò)indexpath獲取指定行的uitableviewcell
    [_table cellForRowAtIndexPath:indexPath];
  • 2: 刪除某行并帶上動(dòng)畫(huà)
    [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  • 3: 刪除某個(gè)section并帶上動(dòng)畫(huà)
[_table deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationMiddle];
  • 4: 取消被選中的某行
    [_table deselectRowAtIndexPath:indexPath animated:YES];
  • 5: 返回某個(gè)section的header自定義view
    [_table headerViewForSection:indexPath.row];
  • 6: 返回某個(gè)section的footer自定義view
    [_table footerViewForSection:indexPath.row];
  • 7: 通過(guò)指定的cell獲取cell所在的行數(shù)
    [_table indexPathForCell:cell];
  • 8: 通過(guò)在table的位置坐標(biāo)返回該坐標(biāo)所在的cell的行數(shù)
    [_table indexPathForRowAtPoint:CGPointMake(0, 0)];
  • 9: 返回被選中的cell的行數(shù)
    [_table indexPathForSelectedRow];
  • 10: 返回table中一個(gè)范圍區(qū)域的cell的行數(shù)
    [_table indexPathsForRowsInRect:CGRectMake(0, 0, 100, 100)];
  • 11: 返回table視覺(jué)區(qū)域內(nèi)的cell的行數(shù)
    [_table indexPathsForVisibleRows];
  • 12: 以動(dòng)畫(huà)方式在指定的位置插入cell
    [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  • 13: 以動(dòng)畫(huà)方式在指定位置插入section
    [_table insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];
  • 14: 返回指定位置的cell的rect屬性
    [_table rectForRowAtIndexPath:indexPath];
  • 15: 重新加載table,重新執(zhí)行所有回調(diào)方法
    [_table reloadData];
  • 16: 指定重新加載table的某些行,并以動(dòng)畫(huà)方式
    [_table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
  • 17: 重新加載索引欄
    [_table reloadSectionIndexTitles];
  • 18: 指定重新加載table的section,并以動(dòng)畫(huà)方式
    [_table reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
  • 19: 滑動(dòng)table到頂部
    [_table scrollsToTop];
  • 20: 帶動(dòng)畫(huà)方式滑動(dòng)table到指定區(qū)間
    [_table scrollRectToVisible:CGRectMake(0, 0, 100, 100) animated:YES];
  • 21: 帶動(dòng)畫(huà)方式滑動(dòng)table到指定位置附近
    [_table scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES];
  • 22: 以動(dòng)畫(huà)方式從table的哪個(gè)指定位置滑動(dòng)到指定的行
    [_table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  • 23: 返回table可視區(qū)域內(nèi)所有的cell
    [_table visibleCells];
  • 24: beginupdates和endupdates要成對(duì)出現(xiàn),對(duì)于同時(shí)操作多個(gè)insert,delete,selection的動(dòng)畫(huà)操作需要把它們放在beginupdates和endupdates中間執(zhí)行,這樣可以把多個(gè)動(dòng)畫(huà)合成一個(gè)動(dòng)畫(huà)來(lái)處理,實(shí)現(xiàn)原子性,降低出錯(cuò)的機(jī)率
      [_table beginUpdates];
      [_table endUpdates];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,315評(píng)論 4 61
  • 一、為什么要有 1、 會(huì)員體系存在的必要 會(huì)員體系作為現(xiàn)在互聯(lián)網(wǎng)平臺(tái)用戶(hù)體系下的重要組成部分,對(duì)商品或服務(wù)的銷(xiāo)售跟...
    歸海軒珠閱讀 1,921評(píng)論 0 8
  • 就是能把蕩秋千這么有情懷,有藝術(shù)氣息,有女神范兒的事情。弄得像個(gè)漢子在蕩秋千,我也是醉了,下次蕩秋千勢(shì)必要裝一下文...
    夏虞姬閱讀 267評(píng)論 5 1
  • 無(wú)紡布袋引領(lǐng)經(jīng)濟(jì)轉(zhuǎn)型升級(jí) 來(lái)源:http://www.hlwfbd.com/ 無(wú)紡布袋引領(lǐng)經(jīng)濟(jì)轉(zhuǎn)型升級(jí) 現(xiàn)今這社會(huì)...
    505521cf3958閱讀 316評(píng)論 0 0
  • 他是葉城城主,少年英俊,在云荒大陸,肩負(fù)中州人的興衰榮辱。 他敢作敢為,天下為謀,獨(dú)自承擔(dān)苦痛,有家臣誓死相隨,堪...
    青衣女閱讀 1,303評(píng)論 0 3

友情鏈接更多精彩內(nèi)容