您的位置:首页 > 产品设计 > UI/UE

UITableView的基本用法

2012-08-30 23:35 344 查看
转自:http://blog.csdn.net/zyc851224/article/details/7879262

几乎大多数的IOS项目中都可以看得到UITableView 的影子,总结了一下,UITableView是iOS开发中,使用最广泛的组件之一,通常都用它来展示一列数据 。开始看一下UITableView的基本语法:

一、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:

此四个方法管理Row的选择. 例如willSelectRowAtIndexPath, 如果此方法返回nil,那么所属的row将无法被选中。

tableView:willBeginEditingRowAtIndexPath:

tableView:didEndEditingRowAtIndexPath:

tableView:editingStyleForRowAtIndexPath:

tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

此四个方法在编辑Row时会被触发。editingStyleForRowAtIndexPath决定Row是否可以被编辑,删除或者移动。targetIndexPathForMoveFromRowAtIndexPath则在移动Row时会把触发,在交换Row位置的时候,必须同时交换DataSource中数据的位置。

示例代码:

//行缩进

-(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

TIPS:自定义选中cell的背景颜色:

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: