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

零: 实例

1: 获取tableviewCell在当前屏幕中的坐标(切换坐标)

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

2: 获取当前所显示的cell的方法:

1.- (NSArray*)visibleCells;
UITableview的方法,这个最直接,返回一个UITableviewcell的数组。
对于自定制的cell,之后的处理可能稍微繁琐些。

2.- (NSArray*)indexPathsForVisibleRows;
UITableview的又一个方法,这个比较好用了,返回一个NSIndexPath的数组,可以直接用indexpath.row去调你的table_related_Array里的数据了。比较方便用于自定制的cell。

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

一: UITableView的datasource代理方法:

  • 1: 回调获取每个section中的cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  • 2: 回调获取每个uitableviewcell,只有当需要显示的cell在table的可视区域内才被回调
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  • 3: 回调获取table的section数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  • 4: 回调获取table的每个section的header标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  • 5: 回调获取table的每个section的footer标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  • 6: 回调判断指定的cell是否能有编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  • 7: 回调判断指定的cell能否被移动当进入编辑模式的时候
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  • 8: 回调获取table右边的索引栏内容
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
  • 9: 当点击右边索引栏时执行的回调,可以根据点击的title值返回应该跳到第几个section
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  • 10: 当所有相关编辑模式的回调,只实现该回调时默认能滑动cell出现删除按钮。当和其他相关的编辑模式回调混合使用分别有移动,插入,删除等功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  • 11: 当进入的是移动的编辑模式时,实现该回调进行移动cell和数据的更新
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

2.UITableView的delegate代理方法:

  • 1: 每个cell将要呈现时回调
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  • 2: 每个section的header将要呈现时回调
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 3: 每个section的footer将要呈现时回调
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 4: 每个cell呈现完毕后回调
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0)
  • 5: 每个section的header呈现完毕后回调
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 6: 每个section的footer呈现完毕后回调
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
  • 7: 回调设置每行的高度,如果要自适应调整cell的高度,则必须要实现该回调,返回调整后的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  • 8: 回调设置每个section的header高度,如果要自适应调整header的高度,则必须要实现该回调,返回调整后的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  • 9: 回调设置每个section的footer高度,如果要自适应调整footer的高度,则必须要实现该回调,返回调整后的footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  • 10: 回调设置每个section的header自定义view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  • 11: 回调设置每个section的footer自定义view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  • 12: 回调设置每行最右边的辅助按钮的样式
/*
 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: 回调设置辅助按钮被点击后的事件
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  • 14: 回调设置某行是否当被点击后处于高亮状态
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
  • 15: 回调当某行处于高亮状态时的行为
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
  • 16: 回调当某行失去高亮状态时的行为
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
  • 17: 回调某行将要被选中的行为
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
  • 18: 回调某行将要被取消选中的行为
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
  • 19: 回调某行已经被选中点击的行为
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  • 20: 回调某行已经取消选中的行为
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
  • 21: 回调设置某行进入了哪种编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  • 22: 回调设置某行进入删除模式的删除按钮名字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
  • 23: 回调设置进入编辑模式的行能否缩进
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
  • 24: 回调将要进入编辑模式的行为
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
  • 25: 回调完成编辑模式的行为
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
  • 26: 回调设置某行的缩进级别
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
  • 27: 回调设置某行被长按是否出现菜单栏
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0)
  • 28: 回调设置菜单栏是否显示哪些菜单栏选项
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)
  • 29: 回调点击菜单栏选项触发的事件
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)

3.UITableView常用成员方法

  • 1: 通过indexpath获取指定行的uitableviewcell
    [_table cellForRowAtIndexPath:indexPath];
  • 2: 删除某行并带上动画
    [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  • 3: 删除某个section并带上动画
[_table deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationMiddle];
  • 4: 取消被选中的某行
    [_table deselectRowAtIndexPath:indexPath animated:YES];
  • 5: 返回某个section的header自定义view
    [_table headerViewForSection:indexPath.row];
  • 6: 返回某个section的footer自定义view
    [_table footerViewForSection:indexPath.row];
  • 7: 通过指定的cell获取cell所在的行数
    [_table indexPathForCell:cell];
  • 8: 通过在table的位置坐标返回该坐标所在的cell的行数
    [_table indexPathForRowAtPoint:CGPointMake(0, 0)];
  • 9: 返回被选中的cell的行数
    [_table indexPathForSelectedRow];
  • 10: 返回table中一个范围区域的cell的行数
    [_table indexPathsForRowsInRect:CGRectMake(0, 0, 100, 100)];
  • 11: 返回table视觉区域内的cell的行数
    [_table indexPathsForVisibleRows];
  • 12: 以动画方式在指定的位置插入cell
    [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  • 13: 以动画方式在指定位置插入section
    [_table insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];
  • 14: 返回指定位置的cell的rect属性
    [_table rectForRowAtIndexPath:indexPath];
  • 15: 重新加载table,重新执行所有回调方法
    [_table reloadData];
  • 16: 指定重新加载table的某些行,并以动画方式
    [_table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
  • 17: 重新加载索引栏
    [_table reloadSectionIndexTitles];
  • 18: 指定重新加载table的section,并以动画方式
    [_table reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
  • 19: 滑动table到顶部
    [_table scrollsToTop];
  • 20: 带动画方式滑动table到指定区间
    [_table scrollRectToVisible:CGRectMake(0, 0, 100, 100) animated:YES];
  • 21: 带动画方式滑动table到指定位置附近
    [_table scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES];
  • 22: 以动画方式从table的哪个指定位置滑动到指定的行
    [_table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  • 23: 返回table可视区域内所有的cell
    [_table visibleCells];
  • 24: beginupdates和endupdates要成对出现,对于同时操作多个insert,delete,selection的动画操作需要把它们放在beginupdates和endupdates中间执行,这样可以把多个动画合成一个动画来处理,实现原子性,降低出错的机率
      [_table beginUpdates];
      [_table endUpdates];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,611评论 4 59
  • 一、为什么要有 1、 会员体系存在的必要 会员体系作为现在互联网平台用户体系下的重要组成部分,对商品或服务的销售跟...
    归海轩珠阅读 1,686评论 0 8
  • 就是能把荡秋千这么有情怀,有艺术气息,有女神范儿的事情。弄得像个汉子在荡秋千,我也是醉了,下次荡秋千势必要装一下文...
    夏虞姬阅读 179评论 5 1
  • 无纺布袋引领经济转型升级 来源:http://www.hlwfbd.com/ 无纺布袋引领经济转型升级 现今这社会...
    505521cf3958阅读 277评论 0 0
  • 他是叶城城主,少年英俊,在云荒大陆,肩负中州人的兴衰荣辱。 他敢作敢为,天下为谋,独自承担苦痛,有家臣誓死相随,堪...
    青衣女阅读 764评论 0 3