UITableView
2016-01-02 00:46
344 查看
在众多移动应用中,能看到各式各样的表格数据
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/14/cb274360b4a73d6cbd756f8e040c93a7)
在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView
UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳
UITableView的两种样式
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/14/6e962f2c406e65424a8db098bda6ebc4)
UITableView需要一个数据源(dataSource)来显示数据。UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等。没有设置数据源的UITableView只是个空壳。凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/14/9d73772438ce64d1026e2f9dd30bc40f)
tableView展示数据的过程
1.调用数据源的下面方法得知一共有多少组数据:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.调用数据源的下面方法得知每一组有多少行数据 :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3. 调用数据源的下面方法得知每一行显示什么内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Cell简介
UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行。UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图。辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图)。
UITableViewCell的contentView
contentView下默认有3个子视图,其中2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)。第3个是UIImageView(通过UITableViewCell的imageView属性访问)。UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/14/9e65a5006aa37c73a18117a565236b59)
UITableView的一些常用属性
UITableView使用示例代码:
#define KWidth [UIScreen mainScreen].bounds.size.width#define KHeight [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableView* myTableView;}
@end
<
e005
p style="margin-top:0px;margin-bottom:0px;line-height:normal;font-family:Helvetica;min-height:14px;">
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; //创建UITableView myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight-49) style:UITableViewStyleGrouped]; myTableView.backgroundColor = [UIColor lightGrayColor]; //设置数据源 myTableView.dataSource = self; //设置代理 myTableView.delegate = self; [self.view addSubview:myTableView]; UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 200)]; headerView.backgroundColor = [UIColor redColor]; myTableView.tableHeaderView = headerView;}
#pragma mark - UITableViewDataSource//tableView里有多少个组-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;}
//每一组有多少个cell-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return 3; }else if (section == 1){ return 2; }else{ return 1; }}
//定义并返回每个cell-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString* myCell = @"myCell"; //从缓存池中取出带有重用标示符的cell UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:myCell]; //如果缓存池中没有带有标示符的cell,则创建生成 if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell]; } if (indexPath.section == 2) { cell.accessoryType = UITableViewCellAccessoryNone; }else{ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } return cell;}
#pragma mark - UITableViewDelegate//每一个cell的高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 54;}
//每一组cell的Header的高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { return 5; }else{ return 35; }}
//每一组cell的Footer的高度-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.000001;}
//点击cell的时候-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES];}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
@end
在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView
UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳
UITableView的两种样式
UITableView需要一个数据源(dataSource)来显示数据。UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等。没有设置数据源的UITableView只是个空壳。凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源。
tableView展示数据的过程
1.调用数据源的下面方法得知一共有多少组数据:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.调用数据源的下面方法得知每一组有多少行数据 :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3. 调用数据源的下面方法得知每一行显示什么内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Cell简介
UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行。UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图。辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图)。
UITableViewCell的contentView
contentView下默认有3个子视图,其中2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)。第3个是UIImageView(通过UITableViewCell的imageView属性访问)。UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置。
UITableView的一些常用属性
//设置UITableView分割线风格 @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle; //设置UITableView分割线颜色,默认为标准灰色 @property(nonatomic,retain) UIColor *separatorColor; //设置UITableView的头部 @property(nonatomic,retain) UIView *tableHeaderView; //设置UITableView的尾部 @property(nonatomic,retain) UIView *tableFooterView; //设置UITableView的Cell的高度 @property (nonatomic) CGFloat rowHeight; //设置UITableView种section的头部的高度 @property (nonatomic) CGFloat sectionHeaderHeight; //设置UITableView种section的尾部的高度 @property (nonatomic) CGFloat sectionFooterHeight; //设置UITableView的背景 @property(nonatomic, readwrite, retain) UIView *backgroundView NS_AVAILABLE_IOS(3_2); //设置UITableView是否可编辑,默认为no,不可编辑 @property(nonatomic,getter=isEditing) BOOL editing; - (void)setEditing:(BOOL)editing animated:(BOOL)animated;//方法带有动画效果 //当UITableView不在编辑时,cell是否可以选中,默认为yes @property(nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0); //当UITableView在编辑时,cell是否可以选中,默认为no @property(nonatomic) BOOL allowsSelectionDuringEditing; //当UITableView不在编辑时,cell是否可以选中多个,默认为no @property(nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0); //当UITableView在编辑时,cell是否可以选中多个,默认为no @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);
UITableView使用示例代码:
#define KWidth [UIScreen mainScreen].bounds.size.width#define KHeight [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableView* myTableView;}
@end
<
e005
p style="margin-top:0px;margin-bottom:0px;line-height:normal;font-family:Helvetica;min-height:14px;">
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; //创建UITableView myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight-49) style:UITableViewStyleGrouped]; myTableView.backgroundColor = [UIColor lightGrayColor]; //设置数据源 myTableView.dataSource = self; //设置代理 myTableView.delegate = self; [self.view addSubview:myTableView]; UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 200)]; headerView.backgroundColor = [UIColor redColor]; myTableView.tableHeaderView = headerView;}
#pragma mark - UITableViewDataSource//tableView里有多少个组-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;}
//每一组有多少个cell-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return 3; }else if (section == 1){ return 2; }else{ return 1; }}
//定义并返回每个cell-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString* myCell = @"myCell"; //从缓存池中取出带有重用标示符的cell UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:myCell]; //如果缓存池中没有带有标示符的cell,则创建生成 if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell]; } if (indexPath.section == 2) { cell.accessoryType = UITableViewCellAccessoryNone; }else{ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } return cell;}
#pragma mark - UITableViewDelegate//每一个cell的高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 54;}
//每一组cell的Header的高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { return 5; }else{ return 35; }}
//每一组cell的Footer的高度-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.000001;}
//点击cell的时候-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES];}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
@end
相关文章推荐
- 使ViewStub 来提高UI的加载的性能
- UIScrollView无法滚动可能的原因及解决办法分析
- UIScrollView的简单使用
- UI_如何实现点击图片之后放大,或缩放功能
- UIScrollView 介绍
- UIScrollView无法滚动可能的原因及解决办法分析
- 代理设计模式在 UIScrollView 中的应用
- UI中如何用纯代码的方式来实现一个图片轮播器
- UISteeper 步进器方法的简单整理
- Android笔记——Handler更新UI示例
- 关于开源框架GPUImage 的简单说明
- Request.ServerVariables["Url"]
- UITextField学习总结
- HDU 3732 Ahui Writes Word(多重背包问题)
- leetcode之Permutation Sequence
- Java 配色方案--Dark Flash Builder - by Wilson Silva
- 搭建MiniGUI开发环境
- Xcode Archive打包时不报错,但是buildFailed原因以及解决方案
- Android Volley完全解析(三),定制自己的Request
- request.getParameter() 中文乱码解决办法