IOS表格视图的制作_第1页
IOS表格视图的制作_第2页
IOS表格视图的制作_第3页
IOS表格视图的制作_第4页
IOS表格视图的制作_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

UITableView表格视图 知识点大纲 1 表格视图简单实用 2 单元格具体内容 3 单元格复用 知识点详解 1 表格视图简单使用 UITableViewStylePlain 普通类型 常用显示大量信息 UITableViewStyleGrouped 分组类型 常用与配置界面 联系人界面 UITableView tableView UITableView alloc initWithFrame self view bounds style UITableViewStylePlain UITableViewDataSource控制表格视图的外观 UITableViewDelegate处理表格视图的事件 tableView数据源在本类本对象中 与数据有关 tableView要显示什么数据 比如有多少行 有多少分区 注意 代理方法中 有两个是必须实现的 没有实现 程 序在启动后就会崩掉 tableView dataSource self 表格视图的代理方法 和数据无关的相关信息 侧重于行为 比如每一行有多 高 tableView delegate self 表格视图如何知道该有多少行 NSInteger tableView UITableView tableView numberOfRowsInSection NSInteger section 每行应该显示什么数据 什么时候调用 在显示单元格的时候调用 UITableViewCell tableView UITableView tableView cellForRowAtIndexPath NSIndexPath indexPath 2 单元格具体内容 注意 为必须实现方法 1 返回组数 2 返回每组的行数 3 返回每行中显示数据 cell indexPath section 哪一组 indexPath row 哪一行 cell的类型 UITableViewCellStyleDefault 默认模式 标题 可选图像 UITableViewCellStyleValue1 标题 可选图像 明细信息 和标题在 同一行 UITableViewCellStyleValue2 标题 明细信息 UITableViewCellStyleSubtitle 标题 可选图像 明细 和标题不在同 一行 UITableViewCell cell UITableViewCell alloc initWithStyle UITableViewCellStyleSubtitle reuseIdentifier nil 标题 cell textLabel text name 明细信息 UITableViewCellStyleValue1类型中 cell detailTextLabel text hero intro 图像 cell imageView image UIImage imageNamed hero icon 背景颜色 会影响到未选中表格行的标签背景 cell backgroundColor UIColor redColor 在实际开发中 使用背景视图的情况比较多 背景视图 不需要指定大小 cell会根据自身的尺寸 自 动填充调整背景视图的显示 UIImage bgImage UIImage imageNamed rr pub button silver stretchableImageWithLeftCapWidth 8 topCapHeight 8 cell backgroundView UIImageView alloc initWithImage bgImage UIView backView UIView alloc init backView backgroundColor UIColor redColor cell backgroundView backView 选中的背景视图 UIView selectedBackImage UIView alloc init selectedBackImage backgroundColor UIColor yellowColor cell selectedBackgroundView selectedBackImage 设置右边按钮 UITableViewCellAccessoryDisclosureIndicator 箭头可 以提示用户 当前行可以点击的 通常选中可以跳转到新页面 UITableViewCellAccessoryCheckmark 对号 通常提示 用户改行设置完毕 比较少用 UITableViewCellAccessoryDetailButton 叹号按钮 通 常点击按钮可以做独立操作 例如alterview UITableViewCellAccessoryDetailDisclosureButton 按钮 右箭头 点击右边叹号按钮的处理方法是在代理方法中 cell accessoryType UITableViewCellAccessoryDisclosureIndicator 自定义右边按钮 UISwitch mySwitch UISwitch alloc init mySwitch addTarget self action selector mySwitchClick forControlEvents UIControlEventValueChanged cell accessoryView mySwitch 如何修改单元格的高度 细节 cell默认的高度是44 44是iOS界面设计中最小 适宜 触控面积 应用场景 很多应用程序 每一行的高度是不一样的 例如新浪微博 每个人发的消息不一样高度就不一样 CGFloat tableView UITableView tableView heightForRowAtIndexPath NSIndexPath indexPath 具体设置哪一行高度 if indexPath section 1 return 44 返回组数 告诉tableView要显示多少组 细节 要是这个方法不写 默认是一组 NSInteger numberOfSectionsInTableView UITableView tableView return dataArray count 作用 返回行数 参数section 表示到底要返回哪一组的行数 NSInteger tableView UITableView tableView numberOfRowsInSection NSInteger section 先根据section获取到子数组 NSArray subArray dataArray section return subArray count 处理某行的点击事件 注意 千万不要写成didDeSelect didDeSelect表示取消 选择 void tableView UITableView tableView didSelectRowAtIndexPath NSIndexPath indexPath UIAlertView alertView UIAlertView alloc init alertView message NSString stringWithFormat 您 点击了 d组 d行 indexPath section indexPath row alertView addButtonWithTitle 取消 alertView show accessoryType为按键时 点击右侧按键的监听事件 此方法不会触发选中行 跟行是相互独立的 只是为accessoryType服务 对定义控件不相应 void tableView UITableView tableView accessoryButtonTappedForRowWithIndexPath NSIndexPath indexPath NSLog 测试accessory 分组标题 表格视图分组类型时显示 NSString tableView UITableView tableView titleForHeaderInSection NSInteger section return 标题 分组脚注 表格视图分组类型时显示 NSString tableView UITableView tableView titleForFooterInSection NSInteger section return 脚注 分组标题的行高 CGFloat tableView UITableView tableView heightForHeaderInSection NSInteger section return 100 分组脚注的行高 CGFloat tableView UITableView tableView heightForFooterInSection NSInteger section if section 1 return 80 return 20 分组标题图片 UIView tableView UITableView tableView viewForHeaderInSection NSInteger section if section 1 UIImageView imageView UIImageView alloc init imageView image UIImage imageNamed img 01 return imageView return nil 分组脚注图片 UIView tableView UITableView tableView viewForFooterInSection NSInteger section if section 3 UIImageView imageView UIImageView alloc init imageView image UIImage imageNamed img 01 return imageView return nil 右边索引 索引数组中的 内容 跟分组无关 索引数组中的下标 对应的是分组下表 NSArray sectionIndexTitlesForTableView UITableView tableView NSMutableArray mArray NSMutableArray alloc init for int i A i Z i mArray addObject NSString stringWithFormat c i return mArray 3 单元格复用 这个方法调用的频率很高 是实现cell明细的 UITableViewCell tableView UITableView tableView cellForRowAtIndexPath NSIndexPath indexPath 单元格循环引用 这样就可以循环使用开始创建出来的 屏幕大小个数的单元格 ID 可重用标示符字符串 static 静态变量 能够保证系统为变量在内存中只分配 一次内存空间 静态变量 一旦创建 就不会被释放 只有当程序被销 毁时 才会释放 static NSString ID Cell 1 去缓存池查找可重用的单元格 UITableViewCell cell tableView dequeueReusableCellWithIdentifier ID 2 如果没找到 if cell nil 实例化新的单元格 cell UITableViewCell alloc initWithStyle UITableViewCellStyleSubtitle reuseIdentifier ID 4 沙盒相关内容 真机调试 var mobile Applications FB2A2FE7 59A3 4F84 AEF9 47B7A40DD121 NSString dirt NSHomeDirectory NSLog dirt var mobile Applications FB2A2FE7 59A3 4F84 AEF9 47B7A40DD121 NSString path NSBundle mainBundle NSLog path path 如何在UI工程中读取文件 例如plist文件 以后iOS工程编译完后变成了一个程序包 bundle iOS系统上一个应用所有的数据都放在一个目录中 不能 访问目录之外的文件 这个目录称为沙盒目录 一句话说明 以后要想读取文件 使用相对路径 获取沙盒目录 NSString homeDir NSHomeDirectory NSLog homeDir homeDir 设置文件位置 NSBundle表示应用包 NSString file NSBundle mainBundle pathForResource bookData ofType plist 读取plist文件 NSArray array NSArray alloc initWithContentsOfFile file NSLog array array dataArray NSMutableArray alloc initWithArray array 5 添加滚动视图 搜索栏 tableView tableHeaderView 放在tableview最顶端 头部视图 tableView tablefooterView 放在table

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论