对首页(九宫格形态)的一个小小优化

现在许多App的首页都是采用的九宫格形式,一级菜单(标题)下面跟二级菜单(图标+标题)。对于这样一个页面来说,collectionView能够极好的适配这样的形式。一级菜单使用section的headView实现,二级菜单使用collectionViewCell实现。

代码类似这样下:

//创建ColletionView
- (void) makeColletionView{
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, STATUSBAR_HEIGHT + 40, SCREEN_WIDTH - 30, SCREEN_HEIGHT - STATUSBAR_HEIGHT - 40) collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview: self.collectionView];
   // [self.collectionView registerNib:[UINib nibWithNibName:@"ShopManageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ShopManageCollectionViewCell"];
    [self.collectionView registerClass:[ShopManageCell class] forCellWithReuseIdentifier:@"cell"];
    //注册头视图
    //[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headview"];
    //注册尾视图
    //[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footview"];
}

- (UICollectionViewFlowLayout *) layout{
    if (_layout == nil) {
        _layout = [[UICollectionViewFlowLayout alloc]init];
        //_layout.itemSize = CGSizeMake((ScreenWidth - 20)/3, 25);
        //_layout.minimumLineSpacing = -20;
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 10, 0);
        _layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    }
    return _layout;
}


//Delegate
//一级菜单
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 3;
}

//二级菜单
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 3;
}

//一级菜单实现
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reuseablebiew = nil;
    
    if ([kind isEqualToString:@"UICollectionElementKindSectionHeader"]) {
        //这是头视图
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[NSString stringWithFormat:@"headview %@",indexPath]];
        UICollectionReusableView *headeview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[NSString stringWithFormat:@"headview %@",indexPath] forIndexPath:indexPath];
        
        //添加Label
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH - 80, 30)];
        headerLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];
        headerLabel.textColor = UIColorFromHex(0x4D4D4D);
        //添加边缘修饰图
        UIImageView *borderView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 6, 2, 16)];
        [borderView setImage:[UIImage imageNamed:@"icon_decorate_blue"]];
        if(indexPath.section == 0){
            headerLabel.text = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Home_DayWork", nil)];
        }else if (indexPath.section == 1){
            headerLabel.text = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Home_Common", nil)];
        }else if (indexPath.section == 2){
            headerLabel.text = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Home_Study", nil)];
        }else{
            headerLabel.text = [NSString stringWithFormat:@" "];
        }
        [headeview addSubview:borderView];
        [headeview addSubview:headerLabel];
        
        reuseablebiew = headeview;
    }
    return reuseablebiew;
    
}

//二级菜单实现
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    ShopManageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopManageCell" forIndexPath:indexPath];
    
    ShopManageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        if(indexPath.row == 0){
            //警情上报
            [cell.icon setImage:[UIImage imageNamed:@"icon_alarm_upload"]];
            [cell.title setText:NSLocalizedString(@"title_alarmUpload", nil)];
        }else if(indexPath.row == 1){
            //协查协办
            [cell.icon setImage:[UIImage imageNamed:@"icon_assist"]];
            [cell.title setText:NSLocalizedString(@"title_assist", nil)];
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            //签到打卡
            [cell.icon setImage:[UIImage imageNamed:@"icon_clockIn"]];
            [cell.title setText:NSLocalizedString(@"title_clockIn", nil)];   
        }else if (indexPath.row == 1) {
            //新闻资讯
            [cell.icon setImage:[UIImage imageNamed:@"icon_news"]];
            [cell.title setText:NSLocalizedString(@"title_news", nil)];
        }else if(indexPath.row == 2){
            //排行榜
            [cell.icon setImage:[UIImage imageNamed:@"icon_ranking_list"]];
            [cell.title setText:NSLocalizedString(@"排行榜", nil)];
        }
    }
    else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            //活动报名
            [cell.icon setImage:[UIImage imageNamed:@"icon_join_activity"]];
            [cell.title setText:NSLocalizedString(@"活动报名", nil)];
        }else  if (indexPath.row == 1) {
           //志愿者申请
           [cell.icon setImage:[UIImage imageNamed:@"icon_volunteer"]];
           [cell.title setText:NSLocalizedString(@"志愿者申请", nil)];
        }else  if (indexPath.row == 2) {
           //安全小知识
           [cell.icon setImage:[UIImage imageNamed:@"icon_safe_knowledge"]];
           [cell.title setText:NSLocalizedString(@"安全小知识", nil)];
        }
    }

    return cell;
}

最近在做一个新项目的时候有这么一个情况,登录之后进入首页有五个角色,每个角色的菜单都会不同。如果只有细微的差别的话,我会选择在Delegate里面添加一些判断。但是我接到的设计方案五个角色菜单的差距还是比较大的。如果要硬写判断可能会有比较大的代码量,且复杂易出错。对阅读代码也会不太友好。所以我想了别的办法控制。

想法一:

仿照多局点的方式,建立多个主页文件(HomePage),在登录后判断完权限以后直接加载对应角色的主页面。这样搞的话至少看起来会舒服一点,没有那么多的判断,但是肉眼可见的增加了代码量和维护成本。如果只有少数几个角色,且角色差距极大我觉得这样的方式还不错。但角色多的话这种方式就不合适了。

想法二:

因为上一个想法的失败,所以回归到只用一个文件来做判断的路子上。那就一定是要写判断的,但能不能不在Delegate里面写判断呢。Delegate里面清清爽爽的才是真香。

然后想到了用dataSource就可以了呀(section表示一级标题,rows表示二级标题),习惯性处理简单主界面的我都快要忘了还有DataSources了。那就开始搞起来吧。cell保持不动,也就我们的dataSources里面需要有icon和title,还需要有一个字段来表示最后的跳转。那就新建一个类来表示吧 homePageModel

最开始我想在homePageModel添加image,title和controller三个字段来适应上面的要求。但动手的时候突然觉得controller可能会有些问题,因为我们跳转的时候可能会传不同的参数。所以决定把这个任务交给主页来做,在

homePageModel里面用枚举定义一个HomePageCode。通过code来判断执行怎样的跳转。

代码如下:

//HomePageModel.h
typedef NS_ENUM(NSInteger , HomePageCode) {
    //日常工作
    HomePage_AlarmUpload = 1,//警情上报
    HomePage_AlarmAccept = 2,//警情受理
    HomePage_AlarmAssist = 3,//协查协办
    HomePage_AlarmDistribute = 4,//协查派发
    HomePage_Patrol = 5,//安防巡逻
    HomePage_PatrolChck = 6,//巡防督查
    //常用功能
    HomePage_ClockIn = 7,//签到打卡
    HomePage_News = 8,//新闻资讯
    HomePage_Scan = 9,//扫一扫
    HomePage_RankingList = 10,//排行榜
    HomePage_StaffManage = 11,//人员管理
    HomePage_VolunteerCheck = 12,//志愿者审核
    //参加活动
    HomePage_Actity = 13,//活动报名
    HomePage_Volunteer = 14,//志愿者申请
    
    //信息处理
    HomePage_InfoSearch = 15,//信息查询
    HomePage_InfoDistribute = 16,//信息发布
    
    //学习培训
    HomePage_SafeKnowledge = 17,//安全小知识
    HomePage_Study = 18,//学习培训
};



@interface HomePageModel : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) UIImage *image;
//@property (nonatomic, strong) UIViewController *controller;
@property (nonatomic, assign) HomePageCode code;

+ (HomePageModel *)addModelWithName:(NSString *)name ImageName:(NSString *)imageName Code:(HomePageCode)code;

@end
//HomePageModel.m
+(HomePageModel *)addModelWithName:(NSString *)name ImageName:(NSString *)imageName Code:(HomePageCode)code{
    HomePageModel *homepage = [[HomePageModel alloc] init];
    homepage.name = getLocalizedString(name, nil);
    homepage.image = [UIImage imageNamed:imageName];
    homepage.code = code;
    return homepage;
}

这时我又有了两个想法:

  • 想法一:再添加一个HomePageGroup(字段有name和array)表示一级菜单,name表示一级菜单的标题,array表示每个一级菜单的内容,即二级菜单。用一个数组来存放一级菜单,数组的count就是一级菜单的数量。
  • 想法二:然后用一个字典来保存每个角色的主页菜单,key<NSString>代表一级菜单, value <NSArray>表示二级菜单。然后就可以判断字段中key的数量来表示一级菜单的数量,内容表示一级菜单的title。通过rows取到二级菜单的详情。

写完之后觉得觉得代码还是太长了,几个角色里面会有相同的功能点,就重复了多次。然后我决定把所有的item对照上面的枚举定义全部写到 HomePageModel 里面。这样就省掉了重复的添加过程。然后再新建一个 HomePageMenu 类来表示菜单整体,定义一个枚举来表示角色名称,并定义一个方法通过角色来获取menu数组。这样在主页里面就可以通过一行代码搞定了。

代码如下:

//  HomePageModel.h
//  菜单功能点配置

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger , HomePageCode) {
    //日常工作
    HomePage_AlarmUpload = 1,//警情上报
    HomePage_AlarmAccept = 2,//警情受理
    HomePage_AlarmAssist = 3,//协查协办
    HomePage_AlarmDistribute = 4,//协查派发
    HomePage_Patrol = 5,//安防巡逻
    HomePage_PatrolChck = 6,//巡防督查
    //常用功能
    HomePage_ClockIn = 7,//签到打卡
    HomePage_News = 8,//新闻资讯
    HomePage_Scan = 9,//扫一扫
    HomePage_RankingList = 10,//排行榜
    HomePage_StaffManage = 11,//人员管理
    HomePage_VolunteerCheck = 12,//志愿者审核
    //参加活动
    HomePage_Actity = 13,//活动报名
    HomePage_Volunteer = 14,//志愿者申请
    
    //信息处理
    HomePage_InfoSearch = 15,//信息查询
    HomePage_InfoDistribute = 16,//信息发布
    
    //学习培训
    HomePage_SafeKnowledge = 17,//安全小知识
    HomePage_Study = 18,//学习培训
};
@protocol HomePageModel <NSObject>

@end

@interface HomePageGroup : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSMutableArray<HomePageModel> *array;

+ (HomePageGroup *)addGroupWithName:(NSString *)name Array:(NSMutableArray<HomePageModel> *)array;
@end

@interface HomePageModel : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) UIViewController *controller;
@property (nonatomic, assign) HomePageCode code;

+ (HomePageModel *)addModelWithName:(NSString *)name ImageName:(NSString *)imageName Code:(HomePageCode)code;

//警情上报
+ (HomePageModel *)AlarmUpload;
//警情受理
+ (HomePageModel *)AlarmAccept;
//协查协办
+ (HomePageModel *)AlarmAssist;
//协查派发
+ (HomePageModel *)AlarmDistribute;
//安防巡逻
+ (HomePageModel *)Patrol;
//巡防督查
+ (HomePageModel *)PatrolCheck;

//签到打卡
+ (HomePageModel *)ClockIn;
//新闻资讯
+ (HomePageModel *)News;
//扫一扫
+ (HomePageModel *)Scan;
//排行榜
+ (HomePageModel *)RankingList;
//人员管理
+ (HomePageModel *)StaffManage;
//志愿者审核
+ (HomePageModel *)VolunteerCheck;

//活动报名
+ (HomePageModel *)Actity;
//志愿者申请
+ (HomePageModel *)Volunteer;

//信息查询
+ (HomePageModel *)InfoSearch;
//信息发布
+ (HomePageModel *)InfoDistribute;

//安全小知识
+ (HomePageModel *)SafeKnowledge;
//学习培训
+ (HomePageModel *)Study;

@end


//  HomePageModel.m

#import "HomePageModel.h"
#import "Util.h"

@implementation HomePageGroup

+ (HomePageGroup *)addGroupWithName:(NSString *)name Array:(NSMutableArray<HomePageModel> *)array{
    HomePageGroup *group = [[HomePageGroup alloc] init];
    group.name = getLocalizedString(name, nil);
    group.array = array;
    
    return group;
}
@end


@implementation HomePageModel

//警情上报
+ (HomePageModel *)AlarmUpload{
    return [HomePageModel addModelWithName:@"title_alarmUpload" ImageName:@"icon_alarm_upload" Code:HomePage_AlarmUpload];
}
//警情受理
+ (HomePageModel *)AlarmAccept{
    return [HomePageModel addModelWithName:@"title_alarmAccept" ImageName:@"icon_alarm_accept" Code:HomePage_AlarmAccept];
}
//协查协办
+ (HomePageModel *)AlarmAssist{
    return [HomePageModel addModelWithName:@"title_assist" ImageName:@"icon_assist" Code:HomePage_AlarmAssist];
}
//协查派发
+ (HomePageModel *)AlarmDistribute{
    return [HomePageModel addModelWithName:@"title_alarmDistribute" ImageName:@"icon_alarm_manage" Code:HomePage_AlarmDistribute];
}
//安防巡逻
+ (HomePageModel *)Patrol{
    return [HomePageModel addModelWithName:@"title_patrol" ImageName:@"icon_patrol" Code:HomePage_Patrol];
}
//巡防督查
+ (HomePageModel *)PatrolCheck{
    return [HomePageModel addModelWithName:@"title_alarmCheck" ImageName:@"icon_patrol_check" Code:HomePage_PatrolChck];
}

//签到打卡
+ (HomePageModel *)ClockIn{
    return [HomePageModel addModelWithName:@"title_clockIn" ImageName:@"icon_clockIn" Code:HomePage_ClockIn];
}
//新闻资讯
+ (HomePageModel *)News{
    return [HomePageModel addModelWithName:@"title_news" ImageName:@"icon_news" Code:HomePage_News];
}
//扫一扫
+ (HomePageModel *)Scan{
    return [HomePageModel addModelWithName:@"title_scan" ImageName:@"icon_home_scan" Code:HomePage_Scan];
}
//排行榜
+ (HomePageModel *)RankingList{
    return [HomePageModel addModelWithName:@"title_rankingList" ImageName:@"icon_ranking_list" Code:HomePage_RankingList];
}
//人员管理
+ (HomePageModel *)StaffManage{
    return [HomePageModel addModelWithName:@"title_staffManage" ImageName:@"icon_chat_group" Code:HomePage_StaffManage];
}
//志愿者审核
+ (HomePageModel *)VolunteerCheck{
    return [HomePageModel addModelWithName:@"title_volunteeerCheck" ImageName:@"icon_volunteer_check" Code:HomePage_VolunteerCheck];
}

//活动报名
+ (HomePageModel *)Actity{
    return [HomePageModel addModelWithName:@"title_activity" ImageName:@"icon_volunteer_check" Code:HomePage_Actity];
}
//志愿者申请
+ (HomePageModel *)Volunteer{
    return [HomePageModel addModelWithName:@"title_volunteeer" ImageName:@"icon_volunteer" Code:HomePage_Volunteer];
}

//信息查询
+ (HomePageModel *)InfoSearch{
    return [HomePageModel addModelWithName:@"title_infoSearch" ImageName:@"icon_info_search" Code:HomePage_InfoSearch];
}
//信息发布
+ (HomePageModel *)InfoDistribute{
    return [HomePageModel addModelWithName:@"title_infoDistribute" ImageName:@"icon_info_distribute" Code:HomePage_InfoDistribute];
}

//安全小知识
+ (HomePageModel *)SafeKnowledge{
    return [HomePageModel addModelWithName:@"title_safeKnowledge" ImageName:@"icon_safe_knowledge" Code:HomePage_SafeKnowledge];
}
//学习培训
+ (HomePageModel *)Study{
    return [HomePageModel addModelWithName:@"title_study" ImageName:@"icon_study" Code:HomePage_Study];
}


+(HomePageModel *)addModelWithName:(NSString *)name ImageName:(NSString *)imageName Code:(HomePageCode)code{
    HomePageModel *homepage = [[HomePageModel alloc] init];
    homepage.name = getLocalizedString(name, nil);//这是个类似NSLocalizedString的方法
    homepage.image = [UIImage imageNamed:imageName];
    homepage.code = code;
    return homepage;
}
@end

//  HomePageMenu.h
//  菜单配置

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger , UserRole) {
    //角色
    UserRole_Volunteer = 1,//志愿者
    UserRole_Propery = 2,//物业人员
    UserRole_ProManage = 3,//物管人员
    UserRole_Police = 4,//民警
    UserRole_PoliceManage = 5,//领导

};

@interface HomePageMenu : NSObject

+(NSMutableArray *)MenuWithRole:(UserRole)role;

@end


//  HomePageMenu.m

#import "HomePageMenu.h"
#import "Util.h"

@implementation HomePageMenu

+(NSMutableArray *)MenuWithRole:(UserRole)role{
    
    NSMutableArray *menuArray = [NSMutableArray array];
    
    if (role == UserRole_Volunteer) {
        //志愿者
        NSMutableArray<HomePageModel> *arrayDayWork = [NSMutableArray<HomePageModel> array];
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上报
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//协查协办
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray<HomePageModel> *arrayCommon = [NSMutableArray<HomePageModel> array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel RankingList]];//排行榜
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray<HomePageModel> *arrayStudy = [NSMutableArray<HomePageModel> array];
        [arrayStudy addObject:[HomePageModel Actity]];//活动报名
        [arrayStudy addObject:[HomePageModel Volunteer]];//志愿者申请
        [arrayStudy addObject:[HomePageModel SafeKnowledge]];//安全小知识
        HomePageGroup *groupStudy = [HomePageGroup addGroupWithName:@"Home_Activity" Array:arrayStudy];
        [menuArray addObjectsFromArray:@[groupDayWork, groupCommon, groupStudy]];

    }else if (role == UserRole_Propery){
        //物业巡逻员
        NSMutableArray<HomePageModel> *arrayDayWork = [NSMutableArray<HomePageModel> array];
        [arrayDayWork addObject:[HomePageModel Patrol]];//安保巡逻
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上报
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//协查协办
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray<HomePageModel> *arrayCommon = [NSMutableArray<HomePageModel> array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel Scan]];//扫一扫
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray<HomePageModel> *arrayStudy = [NSMutableArray<HomePageModel> array];
        [arrayStudy addObject:[HomePageModel SafeKnowledge]];//安全小知识
        [arrayStudy addObject:[HomePageModel Study]];//学习培训
        HomePageGroup *groupStudy = [HomePageGroup addGroupWithName:@"Home_Study" Array:arrayStudy];
        
        [menuArray addObjectsFromArray:@[groupDayWork, groupCommon, groupStudy]];
        
    }else if (role == UserRole_ProManage){
        //物业管理
        NSMutableArray<HomePageModel> *arrayDayWork = [NSMutableArray<HomePageModel> array];
        [arrayDayWork addObject:[HomePageModel Patrol]];//安保巡逻
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上报
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//协查协办
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray<HomePageModel> *arrayCommon = [NSMutableArray<HomePageModel> array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel StaffManage]];//人员管理
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray<HomePageModel> *arrayInfo = [NSMutableArray<HomePageModel> array];
        [arrayInfo addObject:[HomePageModel InfoSearch]];//信息搜索
        [arrayInfo addObject:[HomePageModel InfoDistribute]];//信息发布
        HomePageGroup *groupInfo = [HomePageGroup addGroupWithName:@"Home_Info" Array:arrayInfo];
        
        NSMutableArray<HomePageModel> *arrayStudy = [NSMutableArray<HomePageModel> array];
        [arrayStudy addObject:[HomePageModel SafeKnowledge]];//安全小知识
        [arrayStudy addObject:[HomePageModel Study]];//学习培训
        HomePageGroup *groupStudy = [HomePageGroup addGroupWithName:@"Home_Study" Array:arrayStudy];
        
        [menuArray addObjectsFromArray:@[groupDayWork, groupCommon, groupInfo, groupStudy]];
        
    }else if (role == UserRole_Police){
        //民警
        NSMutableArray<HomePageModel> *arrayDayWork = [NSMutableArray<HomePageModel> array];
        [arrayDayWork addObject:[HomePageModel PatrolCheck]];//督查巡防
        [arrayDayWork addObject:[HomePageModel AlarmAccept]];//警情受理
        [arrayDayWork addObject:[HomePageModel AlarmDistribute]];//协查派发
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray<HomePageModel> *arrayCommon = [NSMutableArray<HomePageModel> array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel VolunteerCheck]];//志愿者审核
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray<HomePageModel> *arrayInfo = [NSMutableArray<HomePageModel> array];
        [arrayInfo addObject:[HomePageModel InfoSearch]];//信息搜索
        [arrayInfo addObject:[HomePageModel InfoDistribute]];//信息发布
        HomePageGroup *groupInfo = [HomePageGroup addGroupWithName:@"Home_Info" Array:arrayInfo];
        
        [menuArray addObjectsFromArray:@[groupDayWork, groupCommon, groupInfo]];
        
    }else if (role == UserRole_PoliceManage){
        //局/政法委领导
        NSMutableArray<HomePageModel> *arrayCommon = [NSMutableArray<HomePageModel> array];
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray<HomePageModel> *arrayInfo = [NSMutableArray<HomePageModel> array];
        [arrayInfo addObject:[HomePageModel InfoSearch]];//信息搜索
        [arrayInfo addObject:[HomePageModel InfoDistribute]];//信息发布
        HomePageGroup *groupInfo = [HomePageGroup addGroupWithName:@"Home_Info" Array:arrayInfo];
        
        [menuArray addObjectsFromArray:@[groupCommon, groupInfo]];
    }
    
    
    
    return menuArray;
}

@end

主页

- (void)initData{
    self.dataSource = [[NSMutableArray alloc] init];
    //获取角色菜单
    self.dataSource = [HomePageMenu MenuWithRole:self.userRole];
}

//Delegata
#pragma collectionView Delegate
//返回分区个数
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    //return 3;
    
    return self.dataSource.count;
}

//返回每个分区的item个数
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    HomePageGroup *group = self.dataSource[section];
    return group.array.count;
    
    //return 3;
}

//返回每个item
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    ShopManageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopManageCell" forIndexPath:indexPath];
    
    ShopManageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    HomePageGroup *group = self.dataSource[indexPath.section];
    HomePageModel *model = group.array[indexPath.row];
    
    [cell.icon setImage:model.image];
    [cell.title setText:model.name];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reuseablebiew = nil;
    
    if ([kind isEqualToString:@"UICollectionElementKindSectionHeader"]) {
        //这是头视图
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[NSString stringWithFormat:@"headview %@",indexPath]];
        UICollectionReusableView *headeview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[NSString stringWithFormat:@"headview %@",indexPath] forIndexPath:indexPath];
        
        //添加Label
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH - 80, 30)];
        headerLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];
        headerLabel.textColor = UIColorFromHex(0x4D4D4D);
        //添加边缘修饰图
        UIImageView *borderView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 6, 2, 16)];
        [borderView setImage:[UIImage imageNamed:@"icon_decorate_blue"]];
        
        HomePageGroup *group = self.dataSource[indexPath.section];
        if (group.name) {
            headerLabel.text = group.name;
        }
        [headeview addSubview:borderView];
        [headeview addSubview:headerLabel];
        
        reuseablebiew = headeview;
    }
    return reuseablebiew;
    
}

//点击事件
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController setNavigationBarHidden:YES];

    HomePageGroup *group = self.dataSource[indexPath.section];
    HomePageModel *model = group.array[indexPath.row];
    
    switch (model.code) {
        case HomePage_Patrol:
            [self.navigationController pushViewController:[[PatrolViewController alloc] init] animated:NO];
            break;
            
        case HomePage_PatrolChck:
            [self.navigationController pushViewController:[[PatrolManageViewController alloc] init] animated:NO];
            break;
            
        default:
            [self.navigationController pushViewController:[[WaitViewController alloc] initWithTitle:@"敬请期待"] animated:NO];
            break;
    }
}

这样就可以了,主页的代码看起来简单多了。其它的工作都放到菜单的配置项里面。可读性也更好了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343