iOS程序-UITableView分组展示数据
来源:互联网 发布:json字符串格式 编辑:程序博客网 时间:2024/05/09 23:12
UITableView分组展示数据,并设置每一组的头部标题和尾部标题。
numberOfSectionsInTableView:设置组数
numberOfRowsInSection:设置每一组的行数
cellForRowAtIndexPath:设置每一行显示的内容和样
titleForHeaderInSection:设置显示的头部标题
titleForFooterInSection:设置显示的尾部标题
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
- iOS程序-UITableView分组展示数据
- iOS程序-UITableView分组展示数据 - 纯手写代码
- IOS UITableView 实现LOL数据展示
- [IOS]UITableView刷新一个分组的数据
- 【iOS】通讯录分组方式展示数据
- ios UItableView,UITableViewHeaderFooterView分组
- UITableView 数据展示
- UITableView 数据展示 cell
- UITableView——实现汽车分组展示
- IOS学习笔记(10)UITableView展示数据 cell删除 构建页眉页脚
- iOS-分组UITableView删除崩溃问题(当删除section中最后一条数据崩溃的情况)
- IOS UITableView分组与索引分区实例
- iOS UItableView通讯录(分组显示)
- IOS UITableView分组与索引分区实例
- 黑马程序员------UITableView数据的展示
- iOS UITableView数据刷新
- 使用ExpandableListView+GridView实现分组数据展示
- iphone开发之表格组件UITableView的使用(二)如何分组展示数据并添加组头和组尾描述
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- Android平板设备上的数字小键盘自定义控件的实现(非弹出软键盘)
- 数据库Sharding的基本思想和切分策略
- stirng转json,String转成List<Map<String,object>>,然后转成json
- 设置TextView里的部分文本的颜色
- iOS程序-UITableView分组展示数据
- ios基础知识整理2
- geekband android #5 第十二周分享(GC、内存优化、过度绘制等)
- Cyclic Nacklace(KMP求循环节)
- Android显示一张很长的图
- 手把手教你写框架之ShykyAnnotations - 工程搭建
- eclipse下使用fiddler进行抓包
- 24点游戏:java的一种穷举实现
- ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify