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控件简单介绍的更多相关文章

  1. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  2. UITableview控件简单介绍

    注意点:数据源方法只能在控制器里设置 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView ...

  3. iOS开发UI篇—Date Picker和UITool Bar控件简单介绍

    iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...

  4. ASP.NET AJAX入门系列(6):UpdateProgress控件简单介绍

    在ASP.NET AJAX Beta2中,UpdateProgress控件已经从“增值”CTP中移到了ASP.NET AJAX核心中.以下两篇关于UpdateProgress的文章基本翻译自ASP.N ...

  5. UISrcoll控件简单介绍

    UISrcoll控件,简单的说就是让界面滑动 当使用uiimageview的时候,给控件设置图片素材时,图片的大小会根据控件的大小,自动做缩放 当使用uibutton的时候,如果是设置背景图,name ...

  6. iOS 开发 ZFUI framework控件,使布局更简单

    来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...

  7. winform基础,主要控件简单介绍,以及小练习

    WinForm - C/S B/S 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执 ...

  8. Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)

    第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试,现在用不着. 点击next  ->creat之 ...

  9. 李洪强iOS开发之拓展篇—UIDynamic(简单介绍)

      iOS开发拓展篇—UIDynamic(简单介绍) 一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能 ...

随机推荐

  1. C#线程安全使用(一)

    关于Task的使用,一直都是半知半解,最近终于有时间详细的看了一遍MSDN,作为备忘录,将心得也记录下来和大家分享. 首先,根据MSDN的描述,Task是FrameWork4引进的新功能,他和ConC ...

  2. Java并发专题(二)线程安全

    前言 随着时代的发展,CPU核数的增加和计算速度的提升,串行化的任务执行显然是对资源的极大浪费,掌握多线程是每个程序员必须掌握的技巧.但是同时多线程也是一把双刃剑,带来了共享资源安全的隐患.在本节会介 ...

  3. WPF 视频教程+笔记

    视频  https://www.bilibili.com/video/av46071366/ 笔记  https://www.cnblogs.com/Time_1990/p/4015716.html

  4. Elasticsearch系列(3):Elasticsearch操作入门

    创建Index 新建Index,可以直接向Elastic服务器发送PUT请求,比如下面的命令创建了一个名为:logdb的Index. [root@elsearchserver ~]# curl -X ...

  5. Dynamics 365测试和启用邮箱时候一直显示“安排电子邮件配置测试”怎么办?

    摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复284或者20181125可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...

  6. Django Windows环境下部署

    环境准备 本文将介绍如何在Windows系统上部署Django web项目,本次部署基于下面的架构: Windows10 64位+Python3.6+Django1.11+Apache2.4+mod_ ...

  7. 容器化系列 - 通过Grafana监测InfluxDB数据 on Docker

    本文演示在Docker中运行Grafana和InfluxDB,并通过Grafana展示InfluxDB曲线图. 1 准备工作 1.1 安装Docker 请参考这里 1.2 下载镜像 $ docker ...

  8. C# 进程间通讯

    扩展阅读:http://www.cnblogs.com/joye-shen/archive/2012/06/16/2551864.html 一.进程间通讯的方式 1)共享内存 包括:内存映射文件,共享 ...

  9. Kafka相关内容总结(Kafka集群搭建手记)

    简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性,但是在设计实现上完全不同,此外它并不是 ...

  10. 基于MFC的学生成绩管理系统的设计与实现

    1.技术介绍MFC是微软基础类库的简称,是微软公司实现的一个C++类库,主要封装了大部分的WINDOWS API函数,并且包含一个应用程序框架,以减少应用程序开发人员工作量.VC++是微软公司开发的C ...