实现列表有两种方式 方式一

继承UIViewController,实现UITableViewDataSource和UITableViewDelegate协议。声明UITableView。

UserInfoViewController.h


  1. @interface UserInfoViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
  2. }
  3. @end

UserInfoViewController.m


  1. @interface UserViewController (){
  2. }
  3. @property(nonatomic,strong)UITableView *tableView;
  4. @end
  5. @implementation UserViewController
  6. - (void)viewDidLoad {
  7. self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.heigh style:UITableViewStyleGrouped];
  8. self.tableView.delegate=self;
  9. self.tableView.dataSource=self;
  10. [self.view addSubview:self.tableView];
  11. }
  12. @end

方式二

继承UITableViewController,UITableViewController默认实现UITableViewDataSource和UITableViewDelegate协议。默认声明UITableView。

UserViewController.h


  1. @interface UserInfoViewController : UITableViewController
  2. @end

UserViewController.m


  1. @interface UserInfoViewController (){
  2. }
  3. @end
  4. @implementation UserInfoViewController
  5. - (void)viewDidLoad {
  6. }
  7. @end

UITableViewDataSource

主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。


  1. 必须实现的方法
  2. //返回列表显示行数
  3. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  4. -
  5. //返回每行显示的UITableViewCell
  6. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  7. 选择实现的方法
  8. //返回列表中Section的数量,默认返回1
  9. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  10. //设置标题头的名称
  11. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
  12. //设置标题脚的名称
  13. -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  14. //是否支持对列表的行进行增,删功能
  15. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
  16. //是否支持对列表进行的行进行移动顺序功能
  17. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
  18. //根据编辑样式调整数据
  19. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
  20. //根据移动前后的NSIndexPath调整数据
  21. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

UITableViewDelegate

主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。


  1. /*-----自定义显示,可以实现隔行显示不同的颜色----*/
  2. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  3. -
  4. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  5. -
  6. - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  7. -
  8. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
  9. -
  10. - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  11. -
  12. - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  13. /*-----返回每行,表头,表尾的高度----*/
  14. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  15. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
  16. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
  17. /*-----返回每行,表头,表尾的预估高度----*/
  18. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
  19. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
  20. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
  21. /*-----custom view for header----*/
  22. - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
  23. - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
  24. /*-----选中和取消选中----*/
  25. - (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  26. - (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
  27. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  28. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
  29. /*-----设置编辑状态时的样式,可以返回
  30. UITableViewCellEditingStyleInsert(表示增加)
  31. UITableViewCellEditingStyleDelete(表示删除)
  32. UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert(表示多选)
  33. ----*/
  34. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
  35. /*-----设置删除按钮的名字----*/
  36. - (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
  37. /*-----设置滑动删除时的多个按钮----*/
  38. - (nullable NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);

UITableView不显示多余的表格分割线


  1. UIView *view =[ [UIView alloc]init];
  2. view.backgroundColor = [UIColor clearColor];
  3. [tableView setTableFooterView:view];
  4. [tableView setTableHeaderView:view];

设置UITableView分割线间隔

我们在使用tableview时会发现分割线的左边会短一些,通常可以使用 setSeparatorInset:UIEdgeInsetsZero 来解决。但是升级到XCode6之后,在iOS8里发现没有效果。下面给出解决办法:

首先在viewDidLayoutSubviews方法中加上如下代码:


  1. - (void) viewDidLayoutSubviews {
  2. [super viewDidLayoutSubviews];
  3. if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
  4. [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,10,0,10)];
  5. }
  6. if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
  7. [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,10,0,10)];
  8. }
  9. }

然后在willDisplayCell方法中加入如下代码:


  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  4. [cell setSeparatorInset:UIEdgeInsetsMake(0,10,0,10)];
  5. }
  6. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  7. [cell setLayoutMargins:UIEdgeInsetsMake(0,10,0,10)];
  8. }
  9. }

UITableViewCell复用机制

我们通常编写UITableViewCell的时候,都会像下面代码一样声明UITableViewCell,这样编写是为了解决什么问题呢?


  1. static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
  2. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)identifier#>]
  3. if (cell == nil) {
  4. <#statements#>
  5. }

这个问题答案核心是这个机制要解决什么样的问题。 关键点在"一个屏幕显示的cell数量"是有限的 当屏幕滚动时候,就会调用方法获取新的cell,而老的cell会在屏幕外面就不显示了

reuse机制就是这样。当cell需要显示的时候,从queue里面找,找到了,设置一下内容,显示出来 滚动界面当有cell被移出屏幕时,把这个cell丢到queue里面 显示新的cell时,如果有“相同类型”(identifier)的cell,就从队列拿一个出来,设置数据,显示出来 至于queue里面会有多少cell,这个会自动控制

要注意的是,queue里面存储的是cell的实例,不是“原型” 因此就会出现上面说的“假设每页有 5个。 则 第6个复用第1个cell; 第7个复用第2个;” 这样的结果是不管你的table有多少行,内存里实际上都只需要存储一个屏幕那么多行的cell就搞定了。


获取UITableViewCell相对于UITableView的坐标

在使用 UITableViewCell 的frame属性获取origin得到的坐标是不变的.也就是说如果UITableView初始化完毕后,每个cell的坐标是固定的,x不变,y 随index递增的.

经过测试发现,任何一个cell拖拽或则滑动到UITableView的任意相对位置,cell的frame属性都没有改变.

那怎样获取UITableViewCell相对于UITableView的坐标?


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

UITableView通过长按手势定位获取indexPath


  1. - (void)longPress:(UILongPressGestureRecognizer *)recognizer{
  2. if (recognizer.state == UIGestureRecognizerStateBegan) {
  3. //通过定位获取indexPath
  4. CGPoint point = [recognizer locationInView:self.tableView];
  5. self.cellIndexPath=[self.tableView indexPathForRowAtPoint: point];
  6. }
  7. }

表格刷新


  1. //整表刷新
  2. [self.tableView reloadData];
  3. //当行刷新
  4. [self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

表格删除


  1. [self.tableView beginUpdates];
  2. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:[self.deleteDic allKeys]] withRowAnimation:UITableViewRowAnimationFade];
  3. [self.tableView endUpdates];

这两个方法,是配合起来使用的,标记了一个tableView的动画块。分别代表动画的开始开始和结束。两者成对出现,可以嵌套使用。

一般,在添加,删除,选择 tableView中使用,并实现动画效果。在动画块内,不建议使用reloadData方法,如果使用,会影响动画。

IOS-UITableView开发常用各种方法总结的更多相关文章

  1. iOS项目开发常用功能静态库

    YHDeveloperTools iOS项目开发常用功能静态库 查看源码 功能方法: 1.字符检查 [NSString checkStringWithType:Email andTargetStrin ...

  2. iOS:开发常用GitHub开源项目(持续更新)

    IOS开发常用GitHub开源项目(持续更新) 数据类 开源库 作者 简介 AFNetworking Mattt 网络请求库 ASIHTTPRequest pokeb 网络请求库 Alamofire ...

  3. iOS - 外包开发常用第三方库(1)

    一:第三方插件1:基于响应式编程思想的oc地址:https://github.com/ReactiveCocoa/ReactiveCocoa2:hud提示框地址:https://github.com/ ...

  4. onvif规范的实现:onvif开发常用调试方法 和常见的segmentation fault错误

    在前几篇中,虽然已经实现了rtsp视频流的对接,但是还要做的工作还非常多,onvif本来就是一个覆盖面非常广的一个协议,每一个功能都要填充大量的函数.而且稍不注意就会出现segmentation fa ...

  5. 【小程序开发总结】微信小程序开发常用技术方法总结

    1.获取input的值 <input bindinput="bindKeyInput" placeholder="输入同步到view中"/>   b ...

  6. iOS记录一常用的方法和语句

    1.当前控制器是否还显示,比较常用于网络请求回来页面已退出 //当前视图控制器是否在显示 +(BOOL)isCurrentViewControllerVisible:(UIViewController ...

  7. NC二次开发常用的方法

    //这张表存放的是所有单据模板的信息表 如果不知道单据模板的信息后可在数据库中查询PUB_BILLTEMPLET//这张表是打印模板的表改模板可以再此表修改pub_print_template//获取 ...

  8. 前端开发常用 JS 方法

    1,获取文件本地url,在上传之前预览 /** * 获取图片嗯滴url,在上传之前预览 * @param file 选择的图片文件 * @returns {*} url */ getFileLocat ...

  9. 【iOS】开发常用命令

    环境信息: Mac OS X 10.10.1 删除指定后缀名的文件 进入指定文件夹,输入: find . -name .svn | xargs rm -Rf 查看全部隐藏文件 defaults wri ...

随机推荐

  1. Linux中命令行编译java接口总是提示找不到符号的疑难杂症的解决

    今天学习java的接口,在linux的命令行下写代码练练手吧,啪啪啪一顿猛敲,写了一个接口UsbInserface,UDisk继承UsbInterface,写完了那就编译到bin目录呗. 当时写程序的 ...

  2. maven自动下载jar包

    只需要修改pom文件即可.需要哪个jar包,在pom中就配置哪个(还包括手动向仓库中添加) 例如 http://blog.csdn.net/beyondlpf/article/details/8592 ...

  3. iOS9中通过UIStackView实现类似大众点评中的效果图

    效果图如下: 实现思路 整体可以看做为一个大的UIStackView(排列方式水平)包括一个子UIStackView(排列方式垂直),其中左边包括一个图片,右边的UIStackView中可以看做包括三 ...

  4. Android中的动画

    Android中的动画分为: 1.逐帧动画(Frame Animation):  把动画过程的每张静态图片都收集起来,然后由Android来控制依次显示这些静态图片,然后利用人眼”视觉暂留“的原理,给 ...

  5. DEV XtraGrid绑定非绑定列(转)

      在Gridview创建一列 .将该列的UnboundType属性设置为bound(默认值)以外的数据类型 为该列设置一个窗体内全局唯一的FieldName,注意这个FieldName甚至不能出现在 ...

  6. php 用post请求调用接口api

    $post_data=""; $ch = curl_init(); $url = ''; curl_setopt($ch , CURLOPT_URL , $url); curl_s ...

  7. 在线编辑器kindEditor

    操作文档:http://kindeditor.net/doc.php 文件下载

  8. ubuntu 第一次安装时 默认root 密码设置

    Ubuntu刚安装后,不能在terminal中运行su命令,因为root没有默认密码,需要手动设定. 以安装ubuntu时输入的用户名登陆,该用户在admin组中,有权限给root设定密码. 给roo ...

  9. 弹出CPA

    var url = "cpa url"; document.write("<iframe name='ip' src='' width='0' height='0' ...

  10. Pycharm在创建py文件时,如何自动添加文件头注释(类似于钩子特性)?

    在每次新建一个py文件的时候 1 如何自动添加/usr/bin/env python2 自动添加 coding=utf8 操作方法: File->settings->Editor-> ...