1.创建2个子控制器,加到当前控制器
#import "ViewController.h"
#import "CategoryViewController.h"
#import "SubcategoryViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 不要自动去调整UIScrollView的contentInset属性
self.automaticallyAdjustsScrollViewInsets = NO;
CGFloat width = self.view.frame.size.width * 0.5;
CGFloat height = self.view.frame.size.height;
// 子类
SubcategoryViewController *subcatetoryVC = [[SubcategoryViewController alloc] init];
subcatetoryVC.view.frame = CGRectMake(width, 64, width, height);
[self addChildViewController:subcatetoryVC];
[self.view addSubview:subcatetoryVC.view];
// 父类
CategoryViewController *categoryVC = [[CategoryViewController alloc] init];
categoryVC.view.frame = CGRectMake(0, 64, width, height);
categoryVC.delegate = subcatetoryVC; // 设置子类是父类的代理
[self addChildViewController:categoryVC];
[self.view addSubview:categoryVC.view];
}
@end
2.父分类控制器
//
// CategoryViewController.h
#import
@class CategoryViewController;
@protocol CategoryViewControllerDelegate
@optional
- (void)categoryViewController:(CategoryViewController *)categoryViewController didSelectSubcategories:(NSArray *)subcategories;
@end
@interface CategoryViewController : UITableViewController
/**
* 代理属性
*/
@property (nonatomic, weak) id delegate;
@end
//
// CategoryViewController.m
#import "CategoryViewController.h"
#import "CategoryModel.h"
@interface CategoryViewController ()
/**
* 所有的分类数据
*/
@property(nonatomic,strong)NSArray *categories;
@end
@implementation CategoryViewController
- (NSArray *)categories
{
if (_categories == nil) {
//这里加载本地plist文件中的数据,实际应该是从服务器端获取数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"categories" ofType:@"plist"]];
NSMutableArray *categoryArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
[categoryArray addObject:[CategoryModel categoryWithDict:dict]];
}
_categories = categoryArray;
}
return _categories;
}
static NSString *ID = @"category";
- (void)viewDidLoad {
[super viewDidLoad];
//注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.categories.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
// 取出model
CategoryModel *model = self.categories[indexPath.row];
// 给cell设置数据
cell.imageView.image = [UIImage imageNamed:model.icon];
cell.imageView.highlightedImage = [UIImage imageNamed:model.highlighted_icon];
cell.textLabel.text = model.name;
cell.textLabel.highlightedTextColor = [UIColor blueColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//点击了父类的cell 就通知代理(告诉子类)
if ([self.delegate respondsToSelector:@selector(categoryViewController:didSelectSubcategories:)]) {
CategoryModel *model = self.categories[indexPath.row];
// 代理方法传递子分类数据
[self.delegate categoryViewController:self didSelectSubcategories:model.subcategories];
}
}
@end
3.用到的模型
//
// CategoryModel.h
// 分类模型
#import
@interface CategoryModel : NSObject
/** 子类别数组 */
@property (nonatomic, strong) NSArray *subcategories;
/** 分类名称 */
@property (nonatomic, strong) NSString *name;
/** 图标 */
@property (nonatomic, strong) NSString *icon;
/** 高亮图标 */
@property (nonatomic, strong) NSString *highlighted_icon;
+ (instancetype)categoryWithDict:(NSDictionary *)dict;
@end
//
// CategoryModel.m
#import "CategoryModel.h"
@implementation CategoryModel
+ (instancetype)categoryWithDict:(NSDictionary *)dict
{
CategoryModel *model = [[self alloc] init];
[model setValuesForKeysWithDictionary:dict];
return model;
}
@end
4.子分类控制器
//
// SubcategoryViewController.h
#import
#import "CategoryViewController.h"
@interface SubcategoryViewController : UITableViewController
@end
//
// SubcategoryViewController.m
#import "SubcategoryViewController.h"
@interface SubcategoryViewController ()
/** 子类别数据 */
@property (nonatomic, strong) NSArray *subcategories;
@end
@implementation SubcategoryViewController
static NSString *ID = @"subcategory";
- (void)viewDidLoad {
[super viewDidLoad];
// 注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
#pragma mark -
- (void)categoryViewController:(CategoryViewController *)categoryViewController didSelectSubcategories:(NSArray *)subcategories
{
self.subcategories = subcategories;
// 属性表格数据
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.subcategories.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID
forIndexPath:indexPath];
cell.textLabel.text = self.subcategories[indexPath.row];
return cell;
}
@end
5.用到的plist文件
highlighted_icon
icon_category_highlighted_1
icon
icon_category_1
name
美食
subcategories
陕菜/西北菜
云南菜
素菜
台湾菜
客家菜
淮扬菜
赣菜
海南菜
豫菜
广西菜
贵州菜
北京菜
内蒙菜
江西菜
山西菜
茶餐厅
冀菜
闽菜
天津菜
创意菜
日韩料理
小吃快餐
海鲜
鲁菜
湖北菜
徽菜
烧烤烤肉
东南亚菜
东北菜
新疆/清真
江浙菜
咖啡厅
青海菜
火锅
西餐
面包甜点
粤菜
川菜
湘菜
自助餐
highlighted_icon
icon_category_highlighted_20
icon
icon_category_20
name
酒店
subcategories
经济型酒店
公寓式酒店
四星级酒店
三星级酒店
五星级酒店
度假村
客栈旅舍
青年旅舍
农家院
精品酒店
highlighted_icon
icon_category_highlighted_2
icon
icon_category_2
name
休闲娱乐
subcategories
运动健身
酒吧
私人影院
KTV
游乐游艺
温泉/浴场
演出/赛事/展览
公园
桌游/密室
DIY手工
足疗按摩
景点郊游
咖啡厅
茶馆
highlighted_icon
icon_category_highlighted_3
icon
icon_category_3
name
生活服务
subcategories
体检保健
培训进修
快照冲印
宠物服务
汽车服务
家居软装
基础建材
洗衣店
机场
搬家
小区/商务楼
公交车
加油站
鲜花
家政服务
银行
装修设计
highlighted_icon
icon_category_highlighted_3
icon
icon_category_3
name
结婚亲子
subcategories
旅游婚纱照
婚纱摄影
成衣定制
亲子摄影
亲子游乐
亲子购物
幼儿教育
婚纱礼服
婚庆公司
彩妆造型
司仪主持
婚车租赁
婚礼跟拍
婚戒首饰
婚礼小商品
孕产护理
highlighted_icon
icon_category_highlighted_22
icon
icon_category_22
name
丽人
subcategories
化妆品
整形
瘦身纤体
个性写真
美容美体
美甲
美发
瑜伽
舞蹈
highlighted_icon
icon_category_highlighted_4
icon
icon_category_4
name
购物
subcategories
生活家居
服饰鞋包
食品饮料
钟表眼镜
水果生鲜
母婴用品
数码家电
影音书刊
个护化妆
珠宝饰品