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

现在许多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代表一级菜单, value 表示二级菜单。然后就可以判断字段中key的数量来表示一级菜单的数量,内容表示一级菜单的title。通过rows取到二级菜单的详情。

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

代码如下:

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

#import 

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 

@end

@interface HomePageGroup : NSObject

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

+ (HomePageGroup *)addGroupWithName:(NSString *)name Array:(NSMutableArray *)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 *)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 

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 *arrayDayWork = [NSMutableArray array];
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上报
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//协查协办
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray *arrayCommon = [NSMutableArray array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel RankingList]];//排行榜
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray *arrayStudy = [NSMutableArray 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 *arrayDayWork = [NSMutableArray array];
        [arrayDayWork addObject:[HomePageModel Patrol]];//安保巡逻
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上报
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//协查协办
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray *arrayCommon = [NSMutableArray array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel Scan]];//扫一扫
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray *arrayStudy = [NSMutableArray 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 *arrayDayWork = [NSMutableArray array];
        [arrayDayWork addObject:[HomePageModel Patrol]];//安保巡逻
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上报
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//协查协办
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray *arrayCommon = [NSMutableArray array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel StaffManage]];//人员管理
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray *arrayInfo = [NSMutableArray array];
        [arrayInfo addObject:[HomePageModel InfoSearch]];//信息搜索
        [arrayInfo addObject:[HomePageModel InfoDistribute]];//信息发布
        HomePageGroup *groupInfo = [HomePageGroup addGroupWithName:@"Home_Info" Array:arrayInfo];
        
        NSMutableArray *arrayStudy = [NSMutableArray 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 *arrayDayWork = [NSMutableArray array];
        [arrayDayWork addObject:[HomePageModel PatrolCheck]];//督查巡防
        [arrayDayWork addObject:[HomePageModel AlarmAccept]];//警情受理
        [arrayDayWork addObject:[HomePageModel AlarmDistribute]];//协查派发
        HomePageGroup *groupDayWork = [HomePageGroup addGroupWithName:@"Home_DayWork" Array:arrayDayWork];
        
        NSMutableArray *arrayCommon = [NSMutableArray array];
        [arrayCommon addObject:[HomePageModel ClockIn]];//签到打卡
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        [arrayCommon addObject:[HomePageModel VolunteerCheck]];//志愿者审核
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray *arrayInfo = [NSMutableArray 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 *arrayCommon = [NSMutableArray array];
        [arrayCommon addObject:[HomePageModel News]];//新闻资讯
        HomePageGroup *groupCommon = [HomePageGroup addGroupWithName:@"Home_Common" Array:arrayCommon];
        
        NSMutableArray *arrayInfo = [NSMutableArray 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;
    }
}

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

你可能感兴趣的:(对首页(九宫格形态)的一个小小优化)