专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

选中分类和子分类

  • CZCategoryViewController 实现 tableViewtableView:didSelectRowAtIndexPath: 方法,来处理 mainTableViewsubTableViewcell 被选中的事件

      - (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
  • CZCategoryViewControllertableView: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]}];
          }
      }
    
  • CZPictureWallViewControllerviewDidLoad 方法里面注册通知,来监听 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);
      }
    
  • categoryPopovercategoryTopItem 定义为属性

      @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];
      }