iOS开发基础-UITableView控件简单介绍
UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动。
UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么内容等。凡是遵守 UITableViewDataSource 协议的Objc对象,都可以是 UITableView 的数据源。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 返回共有多少组数据。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 返回每一组有多少行数据。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 返回每一行显示的内容。
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 返回各组底部显示的标题。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 返回各组头部显示的标题。
实例
新建一个Single View Application,命名为TableViewDemo,首先让 ViewController 类遵守 UITableViewDataSource 协议,并声明一个 UITableView 属性,如下所示:
//ViewController.m
@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
将Table View视图拖到storyboard中,并将其与 tableView 属性建立关联。
接下来设置 tableView 的数据源,修改 viewDidLoad 方法:
//ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
}
重写 UITableViewDataSource 协议的方法:
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%s", __FUNCTION__);
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%s", __FUNCTION__);
if (section == ) {
return ;
} else {
return ;
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (indexPath.section == ) {
if (indexPath.row == ) {
cell.textLabel.text = @"奥迪";
} else if (indexPath.row == ) {
cell.textLabel.text = @"宝马";
}
} else if (indexPath.section == ) {
if (indexPath.row == ) {
cell.textLabel.text = @"本田";
} else if (indexPath.row == ) {
cell.textLabel.text = @"丰田";
} else if (indexPath.row == ) {
cell.textLabel.text = @"马自达";
}
}
return cell;
} - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (section == ) {
return @"高端大气上档次";
} else {
return @"还不错";
}
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == ) {
return @"德系品牌";
} else {
return @"日韩品牌";
}
}
UITableViewCell 有一个 UITableViewCellStyle 属性,用于觉得其 contentView 使用哪些子类型,该属性定义如下:
typedef enum : NSInteger {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;
为显示的内容按组建立数据模型类,命名为 WJQCarGroup ,声明下列属性:
//WJQCarGroup.h
@interface WJQCarGroup : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, strong) NSArray *cars; //当前组所有行的数据
@end
接下来在 ViewController.m 文件中导入 WJQCarGroup.h ,并在类扩展中声明 NSArray 属性用来存放所有组的数据,如下:
//ViewController.m
@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups; //保存所有组的数据
@end
使用懒加载技术重写 carGroups 属性的 getter 方法,如下:
//ViewController.m
#pragma mark - 懒加载
- (NSArray *)carGroups {
if (_carGroups == nil) {
WJQCarGroup *cg1 = [[WJQCarGroup alloc] init];
cg1.title = @"德系品牌";
cg1.desc = @"高端大气上档次";
cg1.cars = @[@"奥迪", @"宝马"]; WJQCarGroup *cg2 = [[WJQCarGroup alloc] init];
cg2.title = @"日韩系列";
cg2.desc = @"还不错";
cg2.cars = @[@"本田", @"丰田"]; WJQCarGroup *cg3 = [[WJQCarGroup alloc] init];
cg3.title = @"欧美品牌";
cg3.desc = @"价格昂贵";
cg3.cars = @[@"劳斯莱斯", @"布加迪", @"小米"];
_carGroups = @[cg1, cg2, cg3];
}
return _carGroups;
}
最后,修改 UITableViewDataSource 协议的相关方法:
//ViewController.m
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%s", __FUNCTION__);
return self.carGroups.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%s", __FUNCTION__);
WJQCarGroup *carGroup = self.carGroups[section];
return carGroup.cars.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
WJQCarGroup *carGroup = self.carGroups[indexPath.section];
NSString *carName = carGroup.cars[indexPath.row];
cell.textLabel.text = carName;
return cell;
} - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
WJQCarGroup *carGroup = self.carGroups[section];
return carGroup.desc;
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
WJQCarGroup *carGroup = self.carGroups[section];
return carGroup.title;
}
参考博客:iOS开发UI篇—UITableview控件简单介绍
实例代码:http://vdisk.weibo.com/s/DiY98QyXCOne0
//ViewController.m
#pragma mark - 控制状态栏是否显示
- (BOOL)prefersStatusBarHidden {
//返回YES代表隐藏状态栏,NO相反
return YES;
}
iOS开发基础-UITableView控件简单介绍的更多相关文章
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- UITableview控件简单介绍
注意点:数据源方法只能在控制器里设置 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView ...
- iOS开发UI篇—Date Picker和UITool Bar控件简单介绍
iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...
- ASP.NET AJAX入门系列(6):UpdateProgress控件简单介绍
在ASP.NET AJAX Beta2中,UpdateProgress控件已经从“增值”CTP中移到了ASP.NET AJAX核心中.以下两篇关于UpdateProgress的文章基本翻译自ASP.N ...
- UISrcoll控件简单介绍
UISrcoll控件,简单的说就是让界面滑动 当使用uiimageview的时候,给控件设置图片素材时,图片的大小会根据控件的大小,自动做缩放 当使用uibutton的时候,如果是设置背景图,name ...
- iOS 开发 ZFUI framework控件,使布局更简单
来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...
- winform基础,主要控件简单介绍,以及小练习
WinForm - C/S B/S 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执 ...
- Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)
第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试,现在用不着. 点击next ->creat之 ...
- 李洪强iOS开发之拓展篇—UIDynamic(简单介绍)
iOS开发拓展篇—UIDynamic(简单介绍) 一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能 ...
随机推荐
- Spring Boot(九)Swagger2自动生成接口文档和Mock模拟数据
一.简介 在当下这个前后端分离的技术趋势下,前端工程师过度依赖后端工程师的接口和数据,给开发带来了两大问题: 问题一.后端接口查看难:要怎么调用?参数怎么传递?有几个参数?参数都代表什么含义? 问题二 ...
- Spring Boot (八)MyBatis + Docker + MongoDB 4.x
一.MongoDB简介 1.1 MongoDB介绍 MongoDB是一个强大.灵活,且易于扩展的通用型数据库.MongoDB是C++编写的文档型数据库,有着丰富的关系型数据库的功能,并在4.0之后添加 ...
- 第38章 刷新令牌 - Identity Server 4 中文文档(v1.0.0)
第38章 刷新令牌 由于访问令牌的生命周期有限,因此刷新令牌允许在没有用户交互的情况下请求新的访问令牌. 以下流程支持刷新令牌:授权代码,混合和资源所有者密码凭据流.需要明确授权客户端通过设置Allo ...
- 别的C#基础笔记
1.方法名称 * 规范:每一个单词的首字母大写 2.方法的返回值 * void:没有返回值.不能使用return来返回具体的值 ,但是可以使用return终止当前方法 ...
- PHP和Go中的闭包变量作用域
关于闭包函数,之前在聊过.这里忽略了一点,不管是Go/Php/Python,闭包都存在局部变量的引用.我们还是先看个例子: PHP示例: $list = []; for ($i = 0; $i < ...
- Oracle day02 函数
order by关键字作用:用于对查询结果进行排序 用法: 1.利用asc .desc对排序列进行升序或降序 2.order by后可以添加多个列(逗号分隔),当一个列的值相同时,在按第二 ...
- Apollo配置中心动态刷新日志级别
Apollo配置中心动态刷新日志级别 添加次配置后,当在apollo上面调整日志级别不需要重启服务器,马上就能生效 /** * 结合apollo动态刷新日志级别 * @author: nj * @da ...
- JSON WEB TOKEN(JWT)的分析
JSON WEB TOKEN(JWT)的分析 一般情况下,客户的会话数据会存在文件中,或者引入redis来存储,实现session的管理,但是这样操作会存在一些问题,使用文件来存储的时候,在多台机器上 ...
- Salesforce 小知识:大量“子记录”的处理方法
大量"子记录"的存放 例子:系统中导入了很多"联系人"(Contact)记录,它们没有具体所属的"客户"(Account)记录.那么我们就要 ...
- 利用efi功能更改bios主板被隐藏的设置(如超频)
整理自(来源): http://tieba.baidu.com/p/4934345324 ([新手教程]利用EFI启动盘修改 隐藏bios设置) http://tieba.baidu.com/p/49 ...