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框架 可以认为是一种物理引擎,能 ...
随机推荐
- 以太仿DApp开发环境搭建
在网上找了些以太仿的资料,是node.js写的,之前也了解过node.js,正好也可以用上.本篇主要学习以太仿DApp开发环境搭建. 一.安装 DApp 开发环境 1.1安装 Node.js 首先下载 ...
- angularjs1.X进阶笔记(3)——如何重构controller
目录 一. 结构拆分 二.基本代码优化 本篇是内部培训交流会的摘要总结. 培训PPT和示例代码已托管至我的github仓库: https://github.com/dashnowords/blogs/ ...
- Unity的四元素与Vector3的乘积的含义
Quaternion.Euler(x,y,z)含义是按照每个旋转轴以x,y,z旋转度旋转,例子:Quaternion.Euler(45,45,45) Quaternion与Vector3的右乘操作(* ...
- 50.Linux-分析ifconfig到内核的调用过程,实现内核启机自动设MAC地址(原)
内核版本: Linux version 3.10.14 1.由于每次开发板开机的网卡eth0的物理地址都是随机的. 然后在网上找到可以通过命令行实现设置mac物理地址: ifconfig eth0 d ...
- Spring Boot 整合 docker
一.什么是docker ? 简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs ...
- Python 面向对象之反射
Python 面向对象之反射 TOC 什么是反射? hasattr getattr setattr delattr 哪些对象可以使用反射 反射的好处 例子一 例子二 什么是反射? 程序可以访问.检查和 ...
- Vue-指令
1. v-text:这个指令用于将vue实例中的data内的属性渲染到标签内.有两种写法: 1. `<div v-text="数据"></div>`:该写法 ...
- SAP MM MI01事务代码里的批次确定
SAP MM MI01事务代码里的批次确定 1 – 批次管理启用之后果 一个物料如果启用了批次管理,那么库存管理以及盘点等诸多事务里都需要在批次的层次上进行. 货物移动的时候,需要在界面上指定相关货物 ...
- 解决Centos7 yum 出现could not retrieve mirrorlist 错误
刚通过VMware12安装了centos7.x后,使用ip addr查看centos局域网的ip发现没有,使用yum安装一些工具包时也出现报错: Loaded plugins: fastestmirr ...
- FocusListener焦点监听器
[FocusListener焦点监听器] public class Demo extends JFrame { public Demo(){ setDefaultCloseOperation(Wind ...