iOS开发:UITableView简单介绍
2015-09-05 20:04
281 查看
iOS开发:UITableView简单介绍
一、UITableView概述
1、继承自UIScrollView,所以可以滚动。
2、表视图的每一条数据都是显示在UITableViewCell对象中。
3、表视图可以分区显示数据,UITableView声明了一个NSIndexPath的类别,主要用来标识当前cell的在tableView中的位置,该类别有section和row两个属性,前者标识当前cell处于第几个section中,后者代表在该section中的第几行。
每个分区称为一个section,每一行称为一个row,编号都是从0开始。
因为UITableView很常用,在iOS视图里很重要,需求也很大,系统给定很多实现的方法,所以需要签订两个协议来进行相关操作:
<UITableViewDataSource, UITableViewDelegate>
_tableView.dataSource = self;
_tableView.delegate = self;
TableView的数据源UITableViewDataSource
TableView的委托UITableViewDelegate
dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。
delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。
二、具体实现
//初始化tableView
_tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
style有两种:UITableViewStyleGrouped 和 UITableViewStylePlain
//初始化数组
_dataSourceArray = [[NSMutableArray alloc]initWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明", @"呼延灼", @"花荣", @"柴进", @"李应", @"朱仝", @"鲁智深", @"武松", @"董平", @"张清", @"杨志", @"徐宁", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊" ,@"阮小二", @"张衡", @"阮小五", @"张顺", @"阮小七", @"杨雄", @"石秀", @"解珍", @"解宝", @"燕青", nil];
协议里必须实现的两个方法:
#pragma mark cell的个数, 即行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataSourceArray.count;
}
#pragma mark cell的样式,即具体内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//设置重用池里cell的标示:
static NSString *identifier = @"Kevin";
//首先去重用池找是否有可利用的cell:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {//if(cell == nil),新建一个cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
*说明:initWithStyle有四种方式
}
//对cell的相关操作
//cell显示内容(应该是数组里的内容)
cell.textLabel.text = [_dataSourceArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"iconfont-dianhua.png"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = @"detailTextLabel";
for (NSInteger i = 0; i < 6; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100 + 35 * i, 0, 30, 20)];
label.backgroundColor = [UIColor greenColor];
label.text = [NSString stringWithFormat:@"%ld", i];
[cell.contentView addSubview:label];
}
return cell;
}
结果如图:
#pragma mark设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
#pragma mark 点击cell时触发的方法(调转到下一界面)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
}
一、UITableView概述
1、继承自UIScrollView,所以可以滚动。
2、表视图的每一条数据都是显示在UITableViewCell对象中。
3、表视图可以分区显示数据,UITableView声明了一个NSIndexPath的类别,主要用来标识当前cell的在tableView中的位置,该类别有section和row两个属性,前者标识当前cell处于第几个section中,后者代表在该section中的第几行。
每个分区称为一个section,每一行称为一个row,编号都是从0开始。
因为UITableView很常用,在iOS视图里很重要,需求也很大,系统给定很多实现的方法,所以需要签订两个协议来进行相关操作:
<UITableViewDataSource, UITableViewDelegate>
_tableView.dataSource = self;
_tableView.delegate = self;
TableView的数据源UITableViewDataSource
TableView的委托UITableViewDelegate
dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。
delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。
二、具体实现
//初始化tableView
_tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
style有两种:UITableViewStyleGrouped 和 UITableViewStylePlain
//初始化数组
_dataSourceArray = [[NSMutableArray alloc]initWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明", @"呼延灼", @"花荣", @"柴进", @"李应", @"朱仝", @"鲁智深", @"武松", @"董平", @"张清", @"杨志", @"徐宁", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊" ,@"阮小二", @"张衡", @"阮小五", @"张顺", @"阮小七", @"杨雄", @"石秀", @"解珍", @"解宝", @"燕青", nil];
协议里必须实现的两个方法:
#pragma mark cell的个数, 即行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataSourceArray.count;
}
#pragma mark cell的样式,即具体内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//设置重用池里cell的标示:
static NSString *identifier = @"Kevin";
//首先去重用池找是否有可利用的cell:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {//if(cell == nil),新建一个cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
*说明:initWithStyle有四种方式
}
//对cell的相关操作
//cell显示内容(应该是数组里的内容)
cell.textLabel.text = [_dataSourceArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"iconfont-dianhua.png"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = @"detailTextLabel";
for (NSInteger i = 0; i < 6; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100 + 35 * i, 0, 30, 20)];
label.backgroundColor = [UIColor greenColor];
label.text = [NSString stringWithFormat:@"%ld", i];
[cell.contentView addSubview:label];
}
return cell;
}
结果如图:
#pragma mark设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
#pragma mark 点击cell时触发的方法(调转到下一界面)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
}
相关文章推荐
- [UIImage resizableImageWithCapInsets:]使用注意
- druid配置(转)
- 蓝懿iOS培训日志3 UI第一课及学习心得
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- DuiLib : 模拟百度网页的填写和搜索
- UIday0901:UITableView 1 表视图的属性和用法 1
- iOS开发:UINavigationController导航控制器
- 有没有被UGUI的锚点萌了一脸~。~
- Android中的Looper,MessageQueue,Handler的理解
- SUID,SGID,SBIT
- UISegmentControl的使用
- IOS--UITextFiled的使用方法详细
- 浙江大学PAT_甲级_1062. Talent and Virtue (25)
- IOS--UIAlertView的使用方法详细
- IOS--UIButton的使用方法详细
- 为UIAlertView添加block支持
- Android UI 调试工具 Hierarchy Viewer
- UITableView 的使用的小技巧
- 在IB中使用Auto Layout来添加UIScrollView
- Week1-2Quick Find