• 需遵守协议 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. ajax中error函数参数详解

    xhr.status和error函数中的status是不一样的,error函数中的status主要包括:"success"."notmodified".&quo ...

  2. 存储过程错误异常处理例子 --> DECLARE EXIT HANDLER FOR SQLEXCEPTION (转)

    刚才一个朋友问到:  mysql  有类似 mssql 退出执行的方法不? 比如我执行到某个条件,下面就终止执行了.  想起以前写的存储过程,找了好久才找到,就发给他,希望对他有所帮助,贴在这里,留作 ...

  3. SPI驱动框架-1(DM8127 Linux2.6.37为例)

    一.驱动程序结构 1.platform_device 文件:/arch/arm/mach-omap2/device.c static struct omap2_mcspi_platform_confi ...

  4. java代码继承super

    总结:多态 :. 当重写父类的方法的时,子类对象名可以调用父类的方法,以及不带参的构造方法 package com.addd; public class rr { int a, b; String c ...

  5. PostgreSQL 9.5 客户端认证

    PostgreSQL 9.5 客户端认证 当一个客户端应用连接一个数据库服务器时,它将指定以哪个PostgreSQL 数据库用户名连接,就像我们以一个特定用户登录一台 Unix 计算机一样.在 SQL ...

  6. jdk中那些常见的类不能被继承的

    对于java中的类,如果是使用final修饰的话,那么这个类就不能够被继承,因为jdk的开发者认为,有一些最基本的类没要必要对开发者开放,如果用户 继承时操作有误,很可能引入很多问题.为了防止用户对基 ...

  7. 链接ssh失败问题

    Starting sshd:/var/empty/sshd must be owned by root and not group or world-writable.                 ...

  8. AndroidStudio 中使用FFMPEG

    1.下载 FFmpeg 源码 git clone https://git.ffmpeg.org/ffmpeg.git 这一步可能会花比较长的时间 2.编译 FFmpeg for Android 2.1 ...

  9. django的render的说明

    return render(request,"homesite.html",locals()) homesite.html页面中的所有内容都可以被渲染,不论是标签还是js代码,包括 ...

  10. DAY16-Django之model

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...