iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍
一、基本介绍
在众多移动应⽤用中,能看到各式各样的表格数据 。
在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,⽽且性能极佳 。
UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置。

二、数据展示
UITableView需要⼀一个数据源(dataSource)来显示数据
UITableView会向数据源查询一共有多少行数据以及每⼀行显示什么数据等
没有设置数据源的UITableView只是个空壳
凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源
展示数据的过程:
(1)调用数据源的下面⽅法得知⼀一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
(2)调用数据源的下面⽅法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
(3)调⽤数据源的下⾯⽅法得知每⼀⾏显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
三、代码示例
(1)能基本展示的“垃圾”代码
#import "NJViewController.h" @interface NJViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation NJViewController - (void)viewDidLoad
{
[super viewDidLoad];
// 设置tableView的数据源
self.tableView.dataSource = self; } #pragma mark - UITableViewDataSource
/**
* 1.告诉tableview一共有多少组
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"numberOfSectionsInTableView");
return ;
}
/**
* 2.第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection %d", section);
if ( == section) {
// 第0组有多少行
return ;
}else
{
// 第1组有多少行
return ;
}
}
/**
* 3.告知系统每一行显示什么内容
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
// indexPath.section; // 第几组
// indexPath.row; // 第几行
// 1.创建cell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; // 2.设置数据
// cell.textLabel.text = @"汽车";
// 判断是第几组的第几行
if ( == indexPath.section) { // 第0组
if ( == indexPath.row) // 第0组第0行
{
cell.textLabel.text = @"奥迪";
}else if ( == indexPath.row) // 第0组第1行
{
cell.textLabel.text = @"宝马";
} }else if ( == indexPath.section) // 第1组
{
if ( == indexPath.row) { // 第0组第0行
cell.textLabel.text = @"本田";
}else if ( == indexPath.row) // 第0组第1行
{
cell.textLabel.text = @"丰田";
}else if ( == indexPath.row) // 第0组第2行
{
cell.textLabel.text = @"马自达";
}
} // 3.返回要显示的控件
return cell; }
/**
* 第section组头部显示什么标题
*
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// return @"标题";
if ( == section) {
return @"德系品牌";
}else
{
return @"日韩品牌";
}
}
/**
* 第section组底部显示什么标题
*
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
if ( == section) {
return @"高端大气上档次";
}else
{
return @"还不错";
}
}
@end
实现效果:

(2)让代码的数据独立
新建一个模型
#import <Foundation/Foundation.h> @interface NJCarGroup : NSObject
/**
* 标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 描述
*/
@property (nonatomic, copy) NSString *desc;
/**
* 当前组所有行的数据
*/
@property (nonatomic, strong) NSArray *cars; @end
#import "NJViewController.h"
#import "NJCarGroup.h" @interface NJViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
* 保存所有组的数据(其中每一元素都是一个模型对象)
*/
@property (nonatomic, strong) NSArray *carGroups;
@end @implementation NJViewController #pragma mark - 懒加载
- (NSArray *)carGroups
{
if (_carGroups == nil) {
// 1.创建模型
NJCarGroup *cg1 = [[NJCarGroup alloc] init];
cg1.title = @"德系品牌";
cg1.desc = @"高端大气上档次";
cg1.cars = @[@"奥迪", @"宝马"]; NJCarGroup *cg2 = [[NJCarGroup alloc] init];
cg2.title = @"日韩品牌";
cg2.desc = @"还不错";
cg2.cars = @[@"本田", @"丰田", @"小田田"]; NJCarGroup *cg3 = [[NJCarGroup alloc] init];
cg3.title = @"欧美品牌";
cg3.desc = @"价格昂贵";
cg3.cars = @[@"劳斯莱斯", @"布加迪", @"小米"];
// 2.将模型添加到数组中
_carGroups = @[cg1, cg2, cg3];
}
// 3.返回数组
return _carGroups;
} - (void)viewDidLoad
{
[super viewDidLoad];
// 设置tableView的数据源
self.tableView.dataSource = self; } #pragma mark - UITableViewDataSource
/**
* 1.告诉tableview一共有多少组
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"numberOfSectionsInTableView");
return self.carGroups.count;
}
/**
* 2.第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection %d", section);
// 1.取出对应的组模型
NJCarGroup *g = self.carGroups[section];
// 2.返回对应组的行数
return g.cars.count;
}
/**
* 3.告知系统每一行显示什么内容
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
// indexPath.section; // 第几组
// indexPath.row; // 第几行
// 1.创建cell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; // 2.设置数据
// cell.textLabel.text = @"嗨喽";
// 2.1取出对应组的模型
NJCarGroup *g = self.carGroups[indexPath.section];
// 2.2取出对应行的数据
NSString *name = g.cars[indexPath.row];
// 2.3设置cell要显示的数据
cell.textLabel.text = name;
// 3.返回要显示的控件
return cell; }
/**
* 第section组头部显示什么标题
*
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// return @"标题";
// 1.取出对应的组模型
NJCarGroup *g = self.carGroups[section];
return g.title;
}
/**
* 第section组底部显示什么标题
*
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
// return @"标题";
// 1.取出对应的组模型
NJCarGroup *g = self.carGroups[section];
return g.desc;
}
@end
实现效果:

提示:请自行体会把数据独立出来单独处理,作为数据模型的好处。另外,把什么抽成一个模型,一定要弄清楚。
四、补充点
contentView下默认有3个⼦视图
第2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)
第3个是UIImageView(通过UITableViewCell的imageView属性访问)
UITableViewCell还有⼀个UITableViewCellStyle属性,⽤于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置

iOS开发UI篇—UITableview控件简单介绍的更多相关文章
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
- iOS开发UI篇—UIScrollView控件实现图片轮播
iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "YYV ...
- 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...
- iOS开发基础-UITableView控件简单介绍
UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动. UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么 ...
- iOS开发Swift篇—(一)简单介绍
iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...
- iOS开发-UI (一)常用控件
从这里开始是UI篇 知识点: 1.常用IOS基本控件 2.UITouch ======================= 常用基本控件 1.UISegmentedControl:分段控制器 1)创建方 ...
随机推荐
- linux--------wdcp中的各种坑。
1.刚买的空间客服给安装了wdcplinux,结果上去一看PHP是5.2版本的,这不是搞笑嘛.然后就有了下面的升级: 复制这条命令回车然后敲Y就可以: wget http://soft.itbulu. ...
- SQLDMO知识总结
SQLDMO(SQL Distributed Management Objects) 参考手册:http://technet.microsoft.com/en-us/library/aa312550( ...
- PoE以太网远程供电
每个以太网口向下挂设备提供的最大功率为 15.4W 通过3/5 类双绞线的信号线(1.3.2.6)同时传递数据和电流
- 最有效地优化 Microsoft SQL Server 的性能
为了最有效地优化 Microsoft SQL Server 的性能,您必须明确当情况不断变化时,性能将在哪些方面得到最大程度的改进,并集中分析这些方面.否则,在这些问题上您可能花费大量的时间和精力 ...
- Openstack命令行创建不同vlan段虚拟机
默认使用nova-network的vlan模式,但是在使用默认的dashboard的时候,不能指定创建的虚拟机的使用网段,固定IP地址. 实际上该功能是在存在的,只是openstack的dashbbo ...
- Objective-C语言继承性
• 继承性是面向对象的重要概念之一,子类能够继承父类的某些方法和成员变量.作用域限定符为private 的成员变量是不可以被继承的.子还可以重写父类的方法. • 继承是单继承,要多继承引入了协议 •子 ...
- MSDTC故障排除
“由于 Microsoft 分布式事务处理协调器出现问题,因此无法连接到配置数据库. 该事务管理器已经禁止了它对远程/网络事务的支持". 第一步: 请确保iis(运行程序的机器)和sql ...
- windows+caffe(一)——自己环境
环境:win7 旗舰版升级到sp1 虚拟机 无GPU vs2013 matlab2016a python2.7 安装caffe已经成功 安装过程见我的另一篇:http://www.cnblogs.co ...
- Lost Cows(线段树 POJ2182)
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10354 Accepted: 6631 Descriptio ...
- ContentProvider总结
一.使用ContentProvider(内容提供者)共享数据 ContentProvider在android中的作用是对外共享数据,也就是说你可以通过ContentProvider把应用中的数据共享给 ...