IOS开发学习笔记026-UITableView的使用
UITableView的简单使用过程
简单介绍
两种样式
UITableViewStylePlain
UITableViewStyleGrouped
数据显示需要设置数据源,数据源是符合遵守协议 <UITableViewDataSource>的类
数据源
dataSource
一些UITableViewDataSource协议里写好的类,这些类有系统自动调用,调用顺序是先设置组再设置行最后设置行内容
设置组section,直接将组返回
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
设置行,直接返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
设置行内容,直接返回UITableViewCell对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
设置组标题(头部),返回头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
设置组描述(尾部),返回尾部描述
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
其中indexPath包含两个数据一个是section一个是row,也就是每一行数据的位置,表示第几组第几行
具体用法看下面的代码
1、创建一个UITableView对象,并设置数据源
// 创建一个UITableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.dataSource = self; // 设置数据源
[self.view addSubview:tableView]; // 添加到视图
既然数据源是self,那么这个类必须遵守协议:UITableViewDataSource,在这个类扩展上遵守协议即可
@interface SLQViewController () <UITableViewDataSource> // 遵守协议 @end
2、设置组
将待添加数据到数组中
@interface SLQViewController () <UITableViewDataSource> // 遵守协议
{
NSArray *_property; // 属性
NSArray *_location; // 位置
}
在viewDidLoad方法中初始化数组
_property = @[@"红",@"蓝",@"黑"];
_location = @[@"上",@"下",@"左",@"右"];
设置有多少组
// 设置组section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ; // 返回组数
}
3、设置每组多少行
// 设置每组多少行 row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == )
return _property.count;
if (section == ) {
return _location.count;
} return ;
}
4、设置第section组第row行的数据
// 设置行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 创建UITableViewCell对象
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (indexPath.section == )
{
cell.textLabel.text = _property[indexPath.row];
}
if (indexPath.section == )
{
cell.textLabel.text = _location[indexPath.row];
} return cell; // 返回
}
5、设置每组头部显示的文字
// 设置每组头部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == )
{
return @"颜色";
}
if (section == )
{
return @"位置";
}
return nil;
}
6、设置每组尾部显示的文字
// 设置每组尾部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
if(section == )
{
return @"设置一些颜色属性啦啦啦啦";
}
if (section == )
{
return @"位置属性的设置哈哈哈哈";
}
return nil;
}
运行可以看到结果:
7、代码优化
上面的代码看着可扩展性太差,下面来几个优化版本,直接看代码吧
//
// SLQViewController.m
// UITableView的练习
//
// Created by Christian on 15/5/16.
// Copyright (c) 2015年 slq. All rights reserved.
// #import "SLQViewController.h" //
#define kHeader @"header"
#define kFooter @"footer"
#define kSetting @"setting" @interface SLQViewController () <UITableViewDataSource> // 遵守协议 {
// NSArray *_property;
// NSArray *_location; // 优化1
// NSArray *_allSetting;
// NSArray *_noramlSet;
// NSArray *_moveSet; // 优化2
NSArray *_allInfo; // 内部保存字典
}
@end @implementation SLQViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建一个UITableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.dataSource = self; // 设置数据源
[self.view addSubview:tableView]; // 添加到视图
//
// _property = @[@"颜色",@"大小",@"透明度"];
// _location = @[@"上",@"下",@"左",@"右"];
// 优化1
// _allSetting = @[
// @[@"颜色",@"大小",@"透明度"],
// @[@"上",@"下",@"左",@"右"],
// ];
// _noramlSet = @[@"设置",@"位置"];
// _moveSet = @[@"常见属性设置啦啦啦啦啦了",@"位移属性设置啊啊啊啊啊啊啊啊"];
// 优化2,保存字典
_allInfo = @[
@{
kHeader : @"颜色",
kFooter : @"颜色属性啦啦啦啦啦啦啦啦啦",
kSetting : @[@"红",@"蓝",@"黑"]
},
@{
kHeader : @"位置",
kFooter : @"位置属性啊啊啊啊啊啊啊啊",
kSetting : @[@"上",@"下",@"左",@"右"]
},
@{
kHeader : @"透明属性",
kFooter : @"透明属性啊啊啊啊啊啊啊啊",
kSetting : @[@"透明",@"半透明",@"不透明"]
}
]; }
// 设置组section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//return 2;
// 优化1
//return _allSetting.count;
// 优化2
return _allInfo.count;
}
// 设置每组多少行 row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// if(section == 0)
// return _property.count;
// if (section == 1) {
// return _location.count;
// }
// 优化1
// return [_allSetting[section] count];
// 优化2
return [_allInfo[section][kSetting] count];
//return 0;
}
// 设置行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// if (indexPath.section == 0)
// {
// cell.textLabel.text = _property[indexPath.row];
// }
// if (indexPath.section == 1)
// {
// cell.textLabel.text = _location[indexPath.row];
// }
// 优化1
//cell.textLabel.text = _allSetting[indexPath.section][indexPath.row];
// 优化2
cell.textLabel.text = _allInfo[indexPath.section][kSetting][indexPath.row];
return cell;
}
// 设置每组头部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// if(section == 0)
// {
// return @"设置";
// }
// if (section == 1)
// {
// return @"位置";
// }
// 优化1
// return _noramlSet[section];
// 优化2
return _allInfo[section][kHeader];
}
// 设置每组尾部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
// if(section == 0)
// {
// return @"设置一些常见属性";
// }
// if (section == 1)
// {
// return @"位置属性的设置";
// }
// 优化1
// return _moveSet[section];
// 优化2
return _allInfo[section][kFooter];
}
@end
源代码:
http://pan.baidu.com/s/1hqCLqra
其实还有优化的空间,继续学习。。。。
最新文章
- CGI, FastCGI, WSGI, uWSGI, uwsgi简述
- [python]python中,使用traceback处理异常信息
- java中调用xml的方法:DocumentBuilderFactory
- 移动混合开发之android文件管理-->;flexbox,webFont。
- maven依赖本地非repository中的jar包
- python网络编程socket /socketserver
- Lua的协程(coroutine)
- Charles是mac的iddler抓包工具
- mysql 基础命令入门学习
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
- node.js在windows下的学习笔记(10)---URL模块
- 你好,C++(29)脚踏两只船的函数不是好函数 5.4 函数设计的基本规则
- UIView--UIImageView
- SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)
- Python之路Day7
- 网站部署到Windows Azure Website上
- 程序启动的目录不一样.ajax请求的地址跳转会出现的问题
- [随笔]利用云虚拟机和学校VPN实现校外访问校内站点(反向代理)
- how tomcat works 读书笔记 十一 StandWrapper 上
- jquery中prop()和attr()用法