• 需遵守协议 UITableViewDataSource, UITableViewDelegate,并设置代理

UITableViewDelegate 继承自 UIScrollViewDelegate

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

1、UITableViewDataSource 和 UITableViewDelegate 协议方法

  • 1.1 分段、行 设置

// 设置分段数,设置 tableView 有多少个分段
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return myDataArray.count;
} // 设置行数,设置 tableView 中每段中有多少行,section 就是第几分段
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[myDataArray objectAtIndex:section] count];
} // 设置行高 ,默认为 44
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60;
} // 设置估计行高
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { /*
只要返回了估计高度,那么就会先调用 tableView:cellForRowAtIndexPath: 方法创建 cell,
再调用 tableView:heightForRowAtIndexPath: 方法获取 cell 的真实高度,
并且显示一个 cell,调用一次 tableView:heightForRowAtIndexPath: 方法。 如果不返回估计高度,会先调用 tableView:heightForRowAtIndexPath: 方法,
再调用 tableView:heightForRowAtIndexPath: 方法,
并且一次性全部调用总 cell 数量次 tableView:heightForRowAtIndexPath: 方法。
*/ return 60;
} // 设置每一行显示的内容,每当有一个 cell 进入视野范围内就会调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return cell;
}
  • 1.2 分段的头、脚标题 设置

// 设置分段的头标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40;
} // 设置分段的脚标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 30;
} // 设置分段的头标题估计高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section { return 40;
} // 设置分段的脚标题估计高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section { return 30;
} // 设置分段的头标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (0 == section) {
return @"1 Header";
}
else{
return @"2 rHeader";
}
} // 设置分段的脚标题内容
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { if (0 == section) {
return @"2 Footer";
}
else{
return @"2 Footer";
}
} // 设置分段头标题视图,返回自定义的标题视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return myView;
} // 设置分段脚标题视图,返回自定义的标题视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return myView;
}
  • 1.3 分段索引条 设置

// 创建索引条
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return array;
} // 设置索引条偏移量,默认索引条与分段一一对应时,可以不写该方法
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
/*
点击索引条上字符串的时候 tableView 会跳转到对应的分段,是根据位置计算的,
点击索引条上第几个,tableView 就会跳到第几段。 如果索引条的前面加了个搜索小图标等,需要重写这个方法。
*/
}
  • 1.4 表格点击 设置

// 表格选中点击响应事件,表格被选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } // 表格取消选中点击响应事件,表格被取消选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { } // 附属控件 button 点击响应事件
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { // 如果系统自带的附属控件里有 button ,附属控件的点击事件会独立出来
}
  • 1.5 表格编辑 设置

// 表格删除、插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 表格删除或插入,默认为删除模式,写入该方法即表示允许删除。
} // 设置编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { // 删除、插入、多选删除,不设置默认时为删除
} // 修改左滑删除按钮的内容
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除";
} // 设置左滑多按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { // 按钮从右向左的顺序排列
return @[action1, action0];
} // 表格移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { }

2、UIScrollViewDelegate 协议方法

  • 2.1 拖拽

// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { } // 将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { } // 已经结束拖拽,decelerate 松手后 是否有惯性滚动 0:没有,1:有
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { }
  • 2.2 滚动

// 滚动过程中,只要滚动就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { } // 已经结束滚动,滚动动画停止时执行,代码改变时触发,也就是 setContentOffset 改变时
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { }
  • 2.3 惯性滚动

// 将要开始惯性滚动
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { } // 已经结束惯性滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { }
  • 2.4 滚到顶端

// 设置点击状态栏时是否滚到顶端
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView { return YES;
} // 已经滚到顶端,点击状态栏时调用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView { }
  • 2.5 缩放

// 设置被缩放的空间,一个 scrollView 中只能有一个子控件被缩放,如果有很多个子控件缩放时会引起错乱
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return [scrollView.subviews[0] viewWithTag:100];
} // 将要开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view { } // 已经结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { } // 缩放过程中,只要缩放就会触发
- (void)scrollViewDidZoom:(UIScrollView *)scrollView { }

tableView 的协议方法的更多相关文章

  1. IOS中tableView每组的头部控件、通过tableView的代理方法控制某一行的cell能否达到高亮选中状态

    一.tableView每组的头部控件 1.控件宽度默认就是tableView的宽度 2.控件高度由下面的代理方法决定 - (CGFloat)tableView:(UITableView *)table ...

  2. UI控件之UIPickerView的协议方法

    UIPickerView:选择视图,父类是UIView UIPickerView *pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(1 ...

  3. 导航栏协议方法UINavigationControllerDelegate

    关于UINavigationControllerDelegate: Delegate中一共有6个方法.其中两个跟控制器ViewController的跳转有关.有两个跟屏幕的旋转有关.有两个跟导航栏动画 ...

  4. 【COCOS2DX-游戏开发之三四】cocos2dx 3.0 TableView特殊使用方法:滚动时不能选择等等

    cocos2dx 3.0版本号TableView拍生自ScrollView,经常使用来做滚动列表,有几种特殊使用方法,不知道大家用到过没 要求:1.滚动时不能选中TableCell,非滚动状态才干选中 ...

  5. 轨迹系列——Socket总结及实现基于TCP或UDP的809协议方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 在上一篇博客中我详细介绍了809协议的内容.809协议规范了通 ...

  6. 轨迹系列7——Socket总结及实现基于TCP或UDP的809协议方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 在上一篇博客中我详细介绍了809协议的内容.809协议规范了通 ...

  7. iOS8中定位服务的变化(CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示)

    最近在写一个LBS的项目的时候,因为考虑到适配iOS8,就将项目迁移到Xcode6.0.1上,出现了不能正常获取定位服务权限的问题. self.manger = [[CLLocationManager ...

  8. appDelegate中的委托协议方法以及使用观察者模式获取其触发方法

    //当应用程序将要进入非活动状态执行,在此期间,应用程序不接受消息或事件,比如来电 - (void)applicationWillResignActive:(UIApplication *)appli ...

  9. ESXI开启snmp协议方法

    公司用VMware做虚拟化,15+HPE 服务器做集群,现需要用zabbix监控其状态,于是想通过打开主机的snmp协议来采集数据,监控其状态,注意其数据是ESXI系统返回的. ssh登录到ESXI上 ...

随机推荐

  1. sleep(0)作用

    假设现在是 2008-4-7 12:00:00.000,如果我调用一下 Thread.Sleep(1000) ,在 2008-4-7 12:00:01.000 的时候,这个线程会 不会被唤醒?某人的代 ...

  2. (转)heX——基于 HTML5 和 Node.JS 开发桌面应用

    本文转载自:http://techblog.youdao.com/?p=685 简介:heX,一个允许你采用前端技术(HTML,CSS,JavaScript)开发桌面应用软件的跨平台解决方案.是你开发 ...

  3. IDEA编译器如何去掉注释中参数错误的提示

    在使用idea的导入别人的项目的时候经常会在方法注释中出现参数错误的提示,这时我们可以参考下面的配置,将方法注释中的参数错误的提示,更新为警告提示~~ 具体使用方法,参考下图~

  4. 为什么in_array(0, ['a', 'b', 'c'])返回true

    为什么in_array(0, ['a', 'b', 'c'])返回true 目录 1 类型转换 2 严格比较 3 false和null 4 数组中有true 在PHP中,数据会自动转换类型后进行比较. ...

  5. Oracle 11g oracle 用户密码过期问题 (ZT)

    http://www.blogjava.net/freeman1984/archive/2013/04/23/398301.html Oracle 11g 之前默认的用户时是没有密码过期的限制的,在O ...

  6. Firefox切换页面默认显示语言

    重新安装了下Firefox,发现页面语言变为中文,而我的有些脚本是在英文界面录的,因此想把默认语言改为英文. 方法如下: 工具 - 选项 - 语言(选择...),将英文上移到顶部 在做上面的修改之前, ...

  7. Python垃圾回收机制:gc模块

    在Python中,为了解决内存泄露问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收. 由于Python 有了自动垃圾回收功能,就造成了不少初学者误认为不必再受内存泄漏的骚扰了.但如果仔细查看一 ...

  8. 使用mui框架后a标签无法跳转

    由于最近工作项目上使用到前台mui框架,笔者在将H5转换为jsp时,遇见各种各样问题,原因归结为对mui框架不熟悉,今天就遇见一个特别奇怪的问题,界面中超链接<a>标签无法跳转,笔者试着添 ...

  9. 第三天:Servlet运行原理

    1.  如何不重启tomcat就可以重新加载一个web应用?? 方法:进入tomcat的manager之后点击reload即可. 2.开发一个Servlet,该Servlet可以输出自己的名字并显示当 ...

  10. dos 下bat命令

    注:cmd下    help > result.txt assoc 显示或修改文件扩展名关联. attrib 显示或更改文件属性. break 设置或清除扩展式 ctrl+c 检查. bcded ...