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

IOS 学习:UITableView 使用详解1

2014-01-12 22:25 676 查看
IOS 学习:UITableView 使用详解1

tableView作为最常见的视图之一在ios应用之中占有举足轻重的地位,因此学习tableView是非常重要的。这篇文章粗略的介绍tableView的创建过程和几个基本的委托方法。

1.添加委托和成员变量。

在视图控制器类的头文件当中,声明UITableView 指针变量table,数组datalList作为数据源。添加UITableViewDelegate 和UITableViewDataSource委托,具体添加方式如下。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSArray *dataList;

@property(nonatomic,retain)UITableView *table;

@end

2.创建UITableView和其他相关对象。

在viewDidLoad方法下添加创建表格试图对象和进行数据源的初始化。

dataList=[[NSArrayalloc]initWithObjects:

@"武汉",

@"重庆",

@"上海",

@"北京",

@"深圳",

@"广州",

@"重庆",

@"香港",

@"台海",

@"天津",nil];

table=[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];

table.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"iphone.jpg"]];

table.delegate=self;

table.dataSource=self;

[self.viewaddSubview:table];

这里重点说一下UITableViewStyle,IOS提供了两种表格样式,即普通样式UITableViewStylePlain和分组样式UITableViewStyleGrouped。

两种样式分别如图:





将表格的delegate和dataSource设置为self,这样我们自定义的委托方法才会被调用。

3.添加委托方法

数据原方法:UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [dataListcount];

}

这个方法返回表格每个分组的行数,非分组的表格默认为一个分组。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return1;

}

这个方法返回表格的组数

表格视图委托方法:UITableViewDelegate

这里的方法都是可选择的,因此可以根据需求使用

这里我只使用了4个方法,效果如上图。

1.创建单元格。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *identifier=@"identifier";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

if(cell==NULL)

{

cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

}

NSInteger row=[indexPath row];

cell.textLabel.text=[dataListobjectAtIndex:row];

return cell;

}

这个函数是具体创建每个单元格所使用的方法, dequeueReusableCellWithIdentifier 方法是检查队列中是否有可重用的单元格,如果没有则进入if条件语句新建单元格。否则跳过新建过程,这样有利于提高表格滚动时的运行速度。

接下来,给每个单元格的标签赋值cell.textLabel.text=[dataList objectAtIndex:row];

然后返回该单元格。

2.呈现单元格之前的设置动作。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

if([indexPath row]%2==0)

{

cell.backgroundColor=[UIColorgreenColor];

}

else{

cell.backgroundColor=[UIColorblueColor];

}

}

当一个单元格将被呈现是,触发此方法,这个方法内部完成个每个单元格的背景颜色设置工作。

当为偶数行市,单元格函数是绿色,否则将是蓝色。

3.表格选择函数。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:dataList[indexPath.row]delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

[alert show];

}

当用户选择某个方法是,将触发此方法。在这个方法内部,新建了一个警告对话框,当选择某个单元格市,将弹出对话框。



deselectRowAtIndexPath方法就是取消选择状态,选择某个单元格后,该单元格变成了蓝色,调用该方法后就会返回未被选择时候所呈现的颜色。

4.滑动删除

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"执行删除");

}

当用户在某个单元格上滑动式时,会出现一个Delete按钮,用户点击该按钮,触发此方法。

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