iOS程序-UITableView分组展示数据 - 纯手写代码
2016-03-30 16:39
465 查看
UITableView分组展示数据,并设置每一组的头部标题和尾部标题。
numberOfSectionsInTableView:设置组数
numberOfRowsInSection:设置每一组的行数
cellForRowAtIndexPath:设置每一行显示的内容和样
titleForHeaderInSection:设置显示的头部标题
titleForFooterInSection:设置显示的尾部标题
MJViewController.h
#import <UIKit/UIKit.h>
@interface MJViewController : UIViewController
@end
MJViewController.m
#import "MJViewController.h"
// 省份字典中用到的key
#define kHeader @"header" // 头部标题对应的key
#define kFooter @"footer" // 尾部标题对应的key
#define kCities @"cities" // 城市数组对应的key
@interface MJViewController () <UITableViewDataSource>
{
// NSArray *_allCities; // 所有的城市
NSArray *_allProvinces; // 所有的省份
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.添加tableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.dataSource = self;
[self.view addSubview:tableView];
// 2.初始化数据
_allProvinces = @[
@{
kHeader : @"广东",
kFooter : @"广东好",
kCities : @[@"广州", @"深圳", @"梅州"]
},
@{
kHeader : @"湖南",
kFooter : @"湖南也好",
kCities : @[@"长沙", @"益阳"]
},
@{
kHeader : @"湖北",
kFooter : @"湖北更好",
kCities : @[@"武汉", @"黄冈"]
}
];
// _allCities = @[
// ,
// ,
// @[@"武汉", @"黄冈"],
// @[@"桂林", @"玉林"],
// @[@"杭州", @"温州"],
// @[@"合肥", @"安庆"]
// ];
}
#pragma mark - 数据源方法
#pragma mark 一共有多少组(section == 区域\组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _allProvinces.count;
}
#pragma mark 第section组一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 1.取得第section组的省份
NSDictionary *province = _allProvinces[section];
// 2.取得省份里面的城市数组
NSArray *cities = province[kCities];
return cities.count;
// // 1.取得第section组的所有城市
// NSArray *sectionCities = _allCities[section];
//
// // 2.第section组城市的个数
// return sectionCities.count;
}
#pragma mark 返回每一行显示的内容(每一行显示怎样的cell)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// NSString *text = _allCities[indexPath.section][indexPath.row];
// NSArray *sectionCities = _allCities[indexPath.section];
// 1.取出第section组第row行的文字数据
// 取出第section组的省份 中 城市数组里面 第 row行的 数据
NSDictionary *province = _allProvinces[indexPath.section];
NSArray *cities = province[kCities];
NSString *text = cities[indexPath.row];
// 2.展示文字数据
cell.textLabel.text = text;
return cell;
}
#pragma mark 第section组显示的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// if (section == 0) return @"广东";
// if (section == 1) return @"湖南";
// if (section == 2) return @"湖北";
// if (section == 3) return @"广西";
// if (section == 4) return @"浙江";
// if (section == 5) return @"安徽";
NSDictionary *province = _allProvinces[section];
return province[kHeader];
}
#pragma mark 第section组显示的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
// if (section == 0) return @"广东好";
// if (section == 1) return @"湖南也好";
// if (section == 2) return @"湖北更好";
// if (section == 3) return @"广西一般般";
// if (section == 4) return @"浙江应该可以吧";
// if (section == 5) return @"安徽确实有点坑爹";
return _allProvinces[section][kFooter];
}
@end
numberOfSectionsInTableView:设置组数
numberOfRowsInSection:设置每一组的行数
cellForRowAtIndexPath:设置每一行显示的内容和样
titleForHeaderInSection:设置显示的头部标题
titleForFooterInSection:设置显示的尾部标题
MJViewController.h
#import <UIKit/UIKit.h>
@interface MJViewController : UIViewController
@end
MJViewController.m
#import "MJViewController.h"
// 省份字典中用到的key
#define kHeader @"header" // 头部标题对应的key
#define kFooter @"footer" // 尾部标题对应的key
#define kCities @"cities" // 城市数组对应的key
@interface MJViewController () <UITableViewDataSource>
{
// NSArray *_allCities; // 所有的城市
NSArray *_allProvinces; // 所有的省份
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.添加tableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.dataSource = self;
[self.view addSubview:tableView];
// 2.初始化数据
_allProvinces = @[
@{
kHeader : @"广东",
kFooter : @"广东好",
kCities : @[@"广州", @"深圳", @"梅州"]
},
@{
kHeader : @"湖南",
kFooter : @"湖南也好",
kCities : @[@"长沙", @"益阳"]
},
@{
kHeader : @"湖北",
kFooter : @"湖北更好",
kCities : @[@"武汉", @"黄冈"]
}
];
// _allCities = @[
// ,
// ,
// @[@"武汉", @"黄冈"],
// @[@"桂林", @"玉林"],
// @[@"杭州", @"温州"],
// @[@"合肥", @"安庆"]
// ];
}
#pragma mark - 数据源方法
#pragma mark 一共有多少组(section == 区域\组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _allProvinces.count;
}
#pragma mark 第section组一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 1.取得第section组的省份
NSDictionary *province = _allProvinces[section];
// 2.取得省份里面的城市数组
NSArray *cities = province[kCities];
return cities.count;
// // 1.取得第section组的所有城市
// NSArray *sectionCities = _allCities[section];
//
// // 2.第section组城市的个数
// return sectionCities.count;
}
#pragma mark 返回每一行显示的内容(每一行显示怎样的cell)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// NSString *text = _allCities[indexPath.section][indexPath.row];
// NSArray *sectionCities = _allCities[indexPath.section];
// 1.取出第section组第row行的文字数据
// 取出第section组的省份 中 城市数组里面 第 row行的 数据
NSDictionary *province = _allProvinces[indexPath.section];
NSArray *cities = province[kCities];
NSString *text = cities[indexPath.row];
// 2.展示文字数据
cell.textLabel.text = text;
return cell;
}
#pragma mark 第section组显示的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// if (section == 0) return @"广东";
// if (section == 1) return @"湖南";
// if (section == 2) return @"湖北";
// if (section == 3) return @"广西";
// if (section == 4) return @"浙江";
// if (section == 5) return @"安徽";
NSDictionary *province = _allProvinces[section];
return province[kHeader];
}
#pragma mark 第section组显示的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
// if (section == 0) return @"广东好";
// if (section == 1) return @"湖南也好";
// if (section == 2) return @"湖北更好";
// if (section == 3) return @"广西一般般";
// if (section == 4) return @"浙江应该可以吧";
// if (section == 5) return @"安徽确实有点坑爹";
return _allProvinces[section][kFooter];
}
@end
相关文章推荐
- QuickTest Professionar 快捷键
- ArrayBlockingQueue源码浅析
- UI设计中ID软件书籍排版时目录的制作你会吗?【太原UI设计师】分享
- iOS开发学习笔记——分段视图(UISegmentedControl)
- 应用自动布局让UITableviewCell自适应高度出现下面问题
- IOS开发-UI学习-UIImageView控件
- UIView的alpha、hidden和opaque属性之间的关系和区别
- 基于QtQuick2.0应用程序运行于XP系统的诸多问题
- 判断Android 系统是MIUI 的,魅族的Flyme ,还是原生的?
- ios UITableView性能优化
- UUID生成方法总结
- UIview需要知道的一些事情:setNeedsDisplay、setNeedsLayout
- Android UI设计——EditText
- HBase源码分析之KeyValue
- QSqlQuery的使用
- Android RoboGuice 使用指南
- 对于requirejs AMD模块加载的理解
- JFinal教程JfinalUIB 代码笔记 (9)--- JFinal分页存在的问题
- Android 之200款UI效果
- UIBarButtonSystemItem样式一览