UITableView基本用法
来源:互联网 发布:linux ubuntu安装教程 编辑:程序博客网 时间:2024/05/18 01:21
几乎大多数的IOS项目中都可以看得到UITableView 的影子,总结了一下,UITableView是iOS开发中,使用最广泛的组件之一,通常都用它来展示一列数据 。开始看一下UITableView的基本语法:
一、UITableView有两个代理协议
Protocol UITableViewDataSource:用来给TableView提供数据
Protocal UITableViewDelegate:控制TableView的展示方式以及事件响应
二、实现代理方法
1、UITableViewDataSource代理方法实现
当然这里还有一些复杂的使用,例如headerView 、 footerView 、titleForHeaderInSection 等等 。
二、UITableViewDelegate的代理方法实现此四个方法管理Row的选择. 例如willSelectRowAtIndexPath, 如果此方法返回nil,那么所属的row将无法被选中。
此四个方法在编辑Row时会被触发。editingStyleForRowAtIndexPath决定Row是否可以被编辑,删除或者移动。targetIndexPathForMoveFromRowAtIndexPath则在移动Row时会把触发,在交换Row位置的时候,必须同时交换DataSource中数据的位置。
示例代码:
三、常用的一些枚举类型选择
TIPS:自定义选中cell的背景颜色:
一、UITableView有两个代理协议
Protocol UITableViewDataSource:用来给TableView提供数据
Protocal UITableViewDelegate:控制TableView的展示方式以及事件响应
二、实现代理方法
1、UITableViewDataSource代理方法实现
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //指定有多少个分区(Section),默认为1
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//指定每个分区中有多少行,默认为1
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //绘制Cell
示例代码:
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return1;//这里使用默认,如果你的数据需要分组显示,在这里就可以定义你所需要的组的个数
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [arrays count];//arrays是你所定义的数据存储数组
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *MyIdentifier = @"MyIdentifier"; //相当于一个行标识
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
- //tableViewCell重绘
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
- }
- NSUInteger row = indexPath.row; //获取行号
- NSString *titleStr = [arrays objectAtIndex:row];//获取数据
- cell.textLabel.text = titleStr;//数据显示
- return cell;
- }
当然这里还有一些复杂的使用,例如headerView 、 footerView 、titleForHeaderInSection 等等 。
- -(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section //设置分区高度
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath //改变行的高度
- -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath //行缩进
二、UITableViewDelegate的代理方法实现
UITableViewDelegate用来管理Row的选择和编辑,有四个方法如下:
- tableView:willSelectRowAtIndexPath:
- tableView:didSelectRowAtIndexPath:
- tableView:willDeselectRowAtIndexPath:
- tableView:didDeselectRowAtIndexPath:
- tableView:willBeginEditingRowAtIndexPath:
- tableView:didEndEditingRowAtIndexPath:
- tableView:editingStyleForRowAtIndexPath:
- tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
示例代码:
- //行缩进
- -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row];
- return row;
- }
- //改变行的高度
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return 40;
- }
三、常用的一些枚举类型选择
- <span style="color:#3366ff;">//选中cell时的颜色</span>
- typedef enum {
- UITableViewCellSelectionStyleNone,
- UITableViewCellSelectionStyleBlue,
- UITableViewCellSelectionStyleGray
- } UITableViewCellSelectionStyle
- <span style="font-size:18px;color:#ff0000;"> cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
- cell.selectedBackgroundView.backgroundColor = [UIColor redColor];</span>
- <span style="color:#3333ff;">//cell右边按钮格式</span>
- typedef enum {
- UITableViewCellAccessoryNone, // don't show any accessory view没有附件
- UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track 黑色向右的箭头
- UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks 蓝色附件按钮
- UITableViewCellAccessoryCheckmark // checkmark. doesn't track 复选框,支持选择
- } UITableViewCellAccessoryType
- //是否加换行线
- typedef enum {
- UITableViewCellSeparatorStyleNone,
- UITableViewCellSeparatorStyleSingleLine
- } UITableViewCellSeparatorStyle
- //改变换行线颜色
- ableView.separatorColor = [UIColor blueColor];
- <span style="font-size:18px;"> //系统提供的UITableView也包含了四种风格的布局
- typedef enum {
- UITableViewCellStyleDefault,
- UITableViewCellStyleValue1,
- UITableViewCellStyleValue2,
- UITableViewCellStyleSubtitle
- } UITableViewCellStyle;</span>
0 0
- UITableView的基本用法
- UITableView基本用法
- UITableview的基本用法
- 32-UITableView基本用法
- 关于UITableView基本用法
- UITableView的基本用法
- UITableView的一些基本用法
- UITableView介绍 之 基本用法
- NavigationController界面跳转,UITableView基本用法
- iOS开发 - UITableview的基本用法
- iOS tableView 控件用法 [0] UITableView的基本用法
- iOS学习笔记-026.UITableView的基本用法
- UITableView用法
- UItableView用法
- UITableView用法
- UITableView 基本使用方法总结
- UITableView 基本使用方法总结
- UITableView 基本使用方法
- 深入理解WeakHashmap
- MTK LCD白屏解决方案
- 什么情况下使用自定义拷贝构造函数
- BC4J
- java程序会发生内存泄露的问题吗
- UITableView基本用法
- android命令
- IOS 图片资源 @2x.png
- Openstack Swift Quotas
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' ......
- live555的一点bug修改(客户端和服务器)
- QQ自动把斜线转为表情了
- SSL协议的分析和实现
- linux共享库位置配置(LD_LIBRARY_PATH环境变量 或者 更改/etc/ld.so.conf 或者 使用-R选项)