专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
选中分类和子分类
在
CZCategoryViewController
实现tableView
的tableView:didSelectRowAtIndexPath:
方法,来处理mainTableView
和subTableView
的cell
被选中的事件- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 获取模型 if (tableView == self.mainTableView) { // 记录mainTableView选中的cell self.selectedMainRow = indexPath.row; // 刷新subTableView的数据 [self.subTableView reloadData]; CZCategoryModel *model = self.categoryVM.categories[self.selectedMainRow]; // 如果选中的分类没有字分类,就发送通知,切换分类 if (model.subcategories.count == 0) { } } else { // 选中subTableView // 发送通知切换分类 // 发送通知切换子分类 } }
创建
CZConst
来专门放置字符串常量CZConst.h
#import <Foundation/Foundation.h> /// 分类发生改变的通知名称 extern NSString *const CZCategoryDidChangeNotification; /// 分类发生改变的通知中获取分类的key extern NSString *const CZCategoryDidChangeKey; /// 分类发生改变的通知中获取子分类的key extern NSString *const CZSubCategoryDidChangeKey;
CZConst.m
#import "CZConst.h" /// 分类发生改变的通知名称 NSString *const CZCategoryDidChangeNotification = @"CZCategoryDidChangeNotification"; /// 分类发生改变的通知中获取分类的key NSString *const CZCategoryDidChangeKey = @"CZCategoryDidChangeKey"; /// 分类发生改变的通知中获取子分类的key NSString *const CZSubCategoryDidChangeKey = @"CZSubCategoryDidChangeKey";
- 在
pch
文件导入CZConst.h
在
CZCategoryViewController
的tableView:didSelectRowAtIndexPath:
方法,发送对应的通知- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 获取模型 if (tableView == self.mainTableView) { // 记录mainTableView选中的cell self.selectedMainRow = indexPath.row; // 刷新subTableView的数据 [self.subTableView reloadData]; CZCategoryModel *model = self.categoryVM.categories[self.selectedMainRow]; // 如果选中的分类没有字分类,就发送通知,切换分类 if (model.subcategories.count == 0) { [[NSNotificationCenter defaultCenter] postNotificationName:CZCategoryDidChangeNotification object:self userInfo:@{CZCategoryDidChangeKey: model.name}]; } } else { CZCategoryModel *model = self.categoryVM.categories[self.selectedMainRow]; // 选中subTableView // 发送通知切换分类 [[NSNotificationCenter defaultCenter] postNotificationName:CZCategoryDidChangeNotification object:self userInfo:@{CZCategoryDidChangeKey: model.name}]; // 发送通知切换子分类 [[NSNotificationCenter defaultCenter] postNotificationName:CZCategoryDidChangeNotification object:self userInfo:@{CZSubCategoryDidChangeKey: model.subcategories[indexPath.row]}]; } }
在
CZPictureWallViewController
的viewDidLoad
方法里面注册通知,来监听CZCategoryDidChangeNotification
通知- (void)viewDidLoad { [super viewDidLoad]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; [self setupLeftNav]; [self setupRightNav]; // 注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(categoryDidChange:) name:CZCategoryDidChangeNotification object:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
- 实现
categoryDidChange:
方法- (void)categoryDidChange:(NSNotification *)notification { CZLog(@"分类改变了%@", notification); }
将
categoryPopover
和categoryTopItem
定义为属性@interface CZPictureWallViewController () @property(nonatomic, strong) UIBarButtonItem *categoryItem; @property(nonatomic, strong) CZTopItem *categoryTopItem; @property(nonatomic, strong) UIBarButtonItem *regionItem; @property(nonatomic, strong) UIPopoverController *categoryPopover; @end
在
categoryDidChange
里面来设置categoryTopItem
的内容- (void)categoryDidChange:(NSNotification *)notification { [self.categoryPopover dismissPopoverAnimated:YES]; // 获取选中的分类 NSString *category = notification.userInfo[CZCategoryDidChangeKey]; if (category) { [self.categoryTopItem setTitle:category]; } // 获取选中的子分类 NSString *subCategory = notification.userInfo[CZSubCategoryDidChangeKey]; [self.categoryTopItem setSubTitle:subCategory]; }