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. python常用脚本以及问题跟踪

    1.时间操作//获取当前时间 格式是%Y-%m-%d %H:%M:%ScurrTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time. ...

  2. 第41章 CORS - Identity Server 4 中文文档(v1.0.0)

    第41章 CORS IdentityServer中的许多端点将通过基于JavaScript的客户端的Ajax调用进行访问.鉴于IdentityServer最有可能托管在与这些客户端不同的源上,这意味着 ...

  3. C#工具:Bootstrap WPF Style,Bootstrap风格的WPF样式

    简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...

  4. FaaS技术框架

    FaaS介绍 微服务(MicroService)是以专注于单一服务/功能的小型单元块为基础,利用模块化的方式组合成复杂的大型应用服务. FaaS是Function as a Service的缩写,可以 ...

  5. bootstrap思考一

    bootstrap是一种热门的Web前端流行框架,如果要兼容PC端.手机端和响应式布局,那他一定是我的首选.bootstrap内容很多,功能强大,其中最好入门也是很重要的就是他的栅格系统.他有四个典型 ...

  6. 外媒评李开复的《AI·未来》:四大浪潮正在席卷全球

    外媒评李开复的<AI·未来>:四大浪潮正在席卷全球 https://mp.weixin.qq.com/s/oElub0QOYjOROhqN3ULUkg [网易智能讯 9月17日消息]李开复 ...

  7. 【Dojo 1.x】笔记4 文字动画效果

    这个笔记,仅仅演示dojo/fx模块的slideTo()方法的简单使用. 有关该模块的用法,见API:有关Dojo的动画.效果,见页面 效果  和  动画 1. 页面组织 html部分同笔记3,js部 ...

  8. AEAI CRM V1.6.0 升级说明,开源客户关系管理系统

    1 升级说明 AEAI CRM v1.6.0版是AEAI CRM v1.5.2版客户关系管理系统的升级版本,本次版本是基于AEAI DP v3.8.0_20170228进行打包部署的,升级内容主要是针 ...

  9. Class.isAssignableFrom与instanceof的区别

    isAssignableFrom 假设有两个类Class1和Class2.Class1.isAssignableFrom(Class2)表示: 类Class1和Class2是否相同. Class1是否 ...

  10. VS2017在线安装包下载

    VS2017个人免费版即社区官方下载地址为:https://download.microsoft.com/download/D/1/4/D142F7E7-4D7E-4F3B-A399-5BACA91E ...