UITableView SDK委托方法详解

UITableView SDK委托方法详解

  今天跟大家分享一下UITableView的各个代理方法的用法,主要是根据SDK里面的介绍,再加上自己的分析与测试,总结成此文。如果有疑问的话,希望大家留言告诉我~

  本文主要讲解对于UITableView最重要的两个协议

  UITableViewDataSource

  UITableViewDelegate


  UITableViewDataSource

  1)tableView:cellForRowAtIndexPath:

  申请一个cell插入到表视图特定的位置,cell生成访问到的方法。

  2)numberOfSectionsInTableView:

  返回表视图的分区数。

  3)tableView:numberOfRowsInSection:

  返回表视图中每个分区的行数。

  4)sectionIndexTitlesForTableView:

  返回分区索引的名称,使用此方法会在表视图右侧创建一个索引栏,通过点击索引可以快速跳转到指定分区。

  5)tableView:sectionForSectionIndexTitle:atIndex:

  点击索引栏会调用此事件,通过点击的标题与索引返回分区的索引。简单来说,就是设定点击右侧索引栏会跳转到的分区,如return 0,那么无论点击什么,都会跳转到分区0。

  6)tableView:titleForHeaderInSection:

  定义每一个分区头的名称。

  7)tableView:titleForFooterInSection:

  定义每一个分区尾的名称。

  8)tableView:commitEditingStyle:forRowAtIndexPath:

  要求数据源提交插入或者删除指定行的事件。即每次删除或者插入完成后都会响应该方法,commitEditingStyle参数标识此次操作是UITableViewCellEditingStyleInsert(插入)

  or UITableViewCellEditingStyleDelete(删除)。

  9)tableView:canEditRowAtIndexPath:

  使得指定行为可编辑状态。

  10)tableView:canMoveRowAtIndexPath:

  使得指定行成可移动状态,如果指定为NO,则该行不会出现可拖动图标。

  11)tableView:moveRowAtIndexPath:toIndexPath:

  移动行之后调用的方法,可以在里面设置表视图数据list的一些操作。

  UITableViewDelegate

  1)tableView:heightForRowAtIndexPath:

  返回行的高度。

  2)tableView:willDisplayCell:forRowAtIndexPath:

  在Cell将要显示时调用此方法,可以在这里修改重用的Cell的一些属性,例如颜色。

  3)tableView:willSelectRowAtIndexPath:

  通知委托指定行被将要选中,返回值为NSIndexPath是指返回响应行的索引,即可以点击一行而返回另一行索引,如果不想点击事件响应则返回nil。

  4)tableView:didSelectRowAtIndexPath:

  通知委托指定行被选中。

  5)tableView:willDeselectRowAtIndexPath:

  通知委托指定行将取消选中。

  6)tableView:didDeselectRowAtIndexPath:

  通知委托指定行被取消选中。

  关于3)到6)的执行顺序:

  willSelectRowAtIndexPath 当前行为:0

  didSelectRowAtIndexPath 当前行为:0

  willSelectRowAtIndexPath 当前行为:1

  willDeselectRowAtIndexPath 当前行为:0

  didDeselectRowAtIndexPath 当前行为:0

  didSelectRowAtIndexPath 当前行为:1

  注:在下一行将要选中后才取消上一行的选中。

   7)tableView:viewForHeaderInSection:

  设置分区的header的视图,可以为它添加图片或者其他控件(UIView的子类)

  8)tableView:viewForFooterInSection:

  设置分区footer的视图,可以为它添加图片或者其他控件(UIView的子类)

  9)tableView:heightForHeaderInSection:

  设置分区header的高度。

  10)tableView:heightForFooterInSection:

  设置分区footer的高度

  11)tableView:willDisplayHeaderView:forSection:

  通知委托将要显示分区header的视图

  12)tableView:willDisplayFooterView:forSection:

  通知委托将要显示分区footer的视图

  13)tableView:willBeginEditingRowAtIndexPath:

  通知委托,表视图将要被编辑(删除或者插入,而是不是编辑cell文字哦)

  14)tableView:didEndEditingRowAtIndexPath:

  表视图完成编辑。

  15)tableView:editingStyleForRowAtIndexPath:

  对当前行设置编辑模式,删除、插入或者不可编辑。

  typedef enum {

  //不可编辑

  UITableViewCellEditingStyleNone,

  //删除  

  UITableViewCellEditingStyleDelete,

  //插入

  UITableViewCellEditingStyleInsert

  }UITableViewCellEditingStyle;

  16)tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:

  在删除模式启动下,改变每行删除按钮的文字(默认为“Delete”)

  17)tableView:shouldIndentWhileEditingRowAtIndexPath:

  通知委托在编辑模式下是否需要对表视图指定行进行缩进,NO为关闭缩进,这个方法可以用来去掉move时row前面的空白。

  18)tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

  移动行的过程中会多次调用此方法,返回值代表进行移动操作后回到的行,如果设置为当前行,则不论怎么移动都会回到当前行。

  19)tableView:didEndDisplayingCell:forRowAtIndexPath:

  通知委托指定的cell已经移出表视图,即拖动的时候移出视图的cell。

  20)tableView:didEndDisplayingHeaderView:forSection:

 通知委托指定的分区头已经移出表视图。

  21)tableView:didEndDisplayingFooterView:forSection:

  通知委托指定的分区尾已经移出视图

  22)tableView:shouldShowMenuForRowAtIndexPath:

  通知委托是否在指定行显示菜单,返回值为YES时,长按显示菜单。

  23)tableView:canPerformAction:forRowAtIndexPath:withSender:

  弹出选择菜单时会调用此方法(复制、粘贴、全选、剪切)

  24)tableView:performAction:forRowAtIndexPath:withSender:

  选择菜单项完成之后调用此方法。

  25)tableView:shouldHighlightRowAtIndexPath:

  通知委托是否开启点击高亮显示,YES为显示。

  26)tableView:didHighlightRowAtIndexPath:

  通知委托表示图的指定行被高亮显示。

  27)tableView:didUnhighlightRowAtIndexPath:

通知委托表视图的指定行不在高亮显示,一般是点击其他行的时候。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值