對(duì)首頁(九宮格形態(tài))的一個(gè)小小優(yōu)化

現(xiàn)在許多App的首頁都是采用的九宮格形式,一級(jí)菜單(標(biāo)題)下面跟二級(jí)菜單(圖標(biāo)+標(biāo)題)啤咽。對(duì)于這樣一個(gè)頁面來說闺魏,collectionView能夠極好的適配這樣的形式非洲。一級(jí)菜單使用section的headView實(shí)現(xiàn),二級(jí)菜單使用collectionViewCell實(shí)現(xiàn)蜕径。

代碼類似這樣下:

//創(chuàng)建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"];
    //注冊(cè)頭視圖
    //[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headview"];
    //注冊(cè)尾視圖
    //[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
//一級(jí)菜單
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 3;
}

//二級(jí)菜單
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 3;
}

//一級(jí)菜單實(shí)現(xiàn)
- (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;
    
}

//二級(jí)菜單實(shí)現(xiàn)
- (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){
            //警情上報(bào)
            [cell.icon setImage:[UIImage imageNamed:@"icon_alarm_upload"]];
            [cell.title setText:NSLocalizedString(@"title_alarmUpload", nil)];
        }else if(indexPath.row == 1){
            //協(xié)查協(xié)辦
            [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) {
            //活動(dòng)報(bào)名
            [cell.icon setImage:[UIImage imageNamed:@"icon_join_activity"]];
            [cell.title setText:NSLocalizedString(@"活動(dòng)報(bào)名", nil)];
        }else  if (indexPath.row == 1) {
           //志愿者申請(qǐng)
           [cell.icon setImage:[UIImage imageNamed:@"icon_volunteer"]];
           [cell.title setText:NSLocalizedString(@"志愿者申請(qǐng)", nil)];
        }else  if (indexPath.row == 2) {
           //安全小知識(shí)
           [cell.icon setImage:[UIImage imageNamed:@"icon_safe_knowledge"]];
           [cell.title setText:NSLocalizedString(@"安全小知識(shí)", nil)];
        }
    }

    return cell;
}

最近在做一個(gè)新項(xiàng)目的時(shí)候有這么一個(gè)情況两踏,登錄之后進(jìn)入首頁有五個(gè)角色,每個(gè)角色的菜單都會(huì)不同兜喻。如果只有細(xì)微的差別的話梦染,我會(huì)選擇在Delegate里面添加一些判斷。但是我接到的設(shè)計(jì)方案五個(gè)角色菜單的差距還是比較大的朴皆。如果要硬寫判斷可能會(huì)有比較大的代碼量帕识,且復(fù)雜易出錯(cuò)。對(duì)閱讀代碼也會(huì)不太友好遂铡。所以我想了別的辦法控制肮疗。

想法一:

仿照多局點(diǎn)的方式,建立多個(gè)主頁文件(HomePage)扒接,在登錄后判斷完權(quán)限以后直接加載對(duì)應(yīng)角色的主頁面伪货。這樣搞的話至少看起來會(huì)舒服一點(diǎn),沒有那么多的判斷钾怔,但是肉眼可見的增加了代碼量和維護(hù)成本碱呼。如果只有少數(shù)幾個(gè)角色,且角色差距極大我覺得這樣的方式還不錯(cuò)宗侦。但角色多的話這種方式就不合適了愚臀。

想法二:

因?yàn)樯弦粋€(gè)想法的失敗,所以回歸到只用一個(gè)文件來做判斷的路子上矾利。那就一定是要寫判斷的姑裂,但能不能不在Delegate里面寫判斷呢。Delegate里面清清爽爽的才是真香男旗。

然后想到了用dataSource就可以了呀(section表示一級(jí)標(biāo)題舶斧,rows表示二級(jí)標(biāo)題),習(xí)慣性處理簡(jiǎn)單主界面的我都快要忘了還有DataSources了剑肯。那就開始搞起來吧捧毛。cell保持不動(dòng),也就我們的dataSources里面需要有icon和title让网,還需要有一個(gè)字段來表示最后的跳轉(zhuǎn)呀忧。那就新建一個(gè)類來表示吧 homePageModel

最開始我想在homePageModel添加image溃睹,title和controller三個(gè)字段來適應(yīng)上面的要求而账。但動(dòng)手的時(shí)候突然覺得controller可能會(huì)有些問題,因?yàn)槲覀兲D(zhuǎn)的時(shí)候可能會(huì)傳不同的參數(shù)因篇。所以決定把這個(gè)任務(wù)交給主頁來做泞辐,在

homePageModel里面用枚舉定義一個(gè)HomePageCode笔横。通過code來判斷執(zhí)行怎樣的跳轉(zhuǎn)。

代碼如下:

//HomePageModel.h
typedef NS_ENUM(NSInteger , HomePageCode) {
    //日常工作
    HomePage_AlarmUpload = 1,//警情上報(bào)
    HomePage_AlarmAccept = 2,//警情受理
    HomePage_AlarmAssist = 3,//協(xié)查協(xié)辦
    HomePage_AlarmDistribute = 4,//協(xié)查派發(fā)
    HomePage_Patrol = 5,//安防巡邏
    HomePage_PatrolChck = 6,//巡防督查
    //常用功能
    HomePage_ClockIn = 7,//簽到打卡
    HomePage_News = 8,//新聞資訊
    HomePage_Scan = 9,//掃一掃
    HomePage_RankingList = 10,//排行榜
    HomePage_StaffManage = 11,//人員管理
    HomePage_VolunteerCheck = 12,//志愿者審核
    //參加活動(dòng)
    HomePage_Actity = 13,//活動(dòng)報(bào)名
    HomePage_Volunteer = 14,//志愿者申請(qǐng)
    
    //信息處理
    HomePage_InfoSearch = 15,//信息查詢
    HomePage_InfoDistribute = 16,//信息發(fā)布
    
    //學(xué)習(xí)培訓(xùn)
    HomePage_SafeKnowledge = 17,//安全小知識(shí)
    HomePage_Study = 18,//學(xué)習(xí)培訓(xùn)
};



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

這時(shí)我又有了兩個(gè)想法:

  • 想法一:再添加一個(gè)HomePageGroup(字段有name和array)表示一級(jí)菜單咐吼,name表示一級(jí)菜單的標(biāo)題吹缔,array表示每個(gè)一級(jí)菜單的內(nèi)容,即二級(jí)菜單锯茄。用一個(gè)數(shù)組來存放一級(jí)菜單厢塘,數(shù)組的count就是一級(jí)菜單的數(shù)量。
  • 想法二:然后用一個(gè)字典來保存每個(gè)角色的主頁菜單肌幽,key<NSString>代表一級(jí)菜單晚碾, value <NSArray>表示二級(jí)菜單。然后就可以判斷字段中key的數(shù)量來表示一級(jí)菜單的數(shù)量喂急,內(nèi)容表示一級(jí)菜單的title格嘁。通過rows取到二級(jí)菜單的詳情。

寫完之后覺得覺得代碼還是太長(zhǎng)了廊移,幾個(gè)角色里面會(huì)有相同的功能點(diǎn)糕簿,就重復(fù)了多次。然后我決定把所有的item對(duì)照上面的枚舉定義全部寫到 HomePageModel 里面画机。這樣就省掉了重復(fù)的添加過程冶伞。然后再新建一個(gè) HomePageMenu 類來表示菜單整體,定義一個(gè)枚舉來表示角色名稱步氏,并定義一個(gè)方法通過角色來獲取menu數(shù)組。這樣在主頁里面就可以通過一行代碼搞定了徒爹。

代碼如下:

//  HomePageModel.h
//  菜單功能點(diǎn)配置

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger , HomePageCode) {
    //日常工作
    HomePage_AlarmUpload = 1,//警情上報(bào)
    HomePage_AlarmAccept = 2,//警情受理
    HomePage_AlarmAssist = 3,//協(xié)查協(xié)辦
    HomePage_AlarmDistribute = 4,//協(xié)查派發(fā)
    HomePage_Patrol = 5,//安防巡邏
    HomePage_PatrolChck = 6,//巡防督查
    //常用功能
    HomePage_ClockIn = 7,//簽到打卡
    HomePage_News = 8,//新聞資訊
    HomePage_Scan = 9,//掃一掃
    HomePage_RankingList = 10,//排行榜
    HomePage_StaffManage = 11,//人員管理
    HomePage_VolunteerCheck = 12,//志愿者審核
    //參加活動(dòng)
    HomePage_Actity = 13,//活動(dòng)報(bào)名
    HomePage_Volunteer = 14,//志愿者申請(qǐng)
    
    //信息處理
    HomePage_InfoSearch = 15,//信息查詢
    HomePage_InfoDistribute = 16,//信息發(fā)布
    
    //學(xué)習(xí)培訓(xùn)
    HomePage_SafeKnowledge = 17,//安全小知識(shí)
    HomePage_Study = 18,//學(xué)習(xí)培訓(xùn)
};
@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;

//警情上報(bào)
+ (HomePageModel *)AlarmUpload;
//警情受理
+ (HomePageModel *)AlarmAccept;
//協(xié)查協(xié)辦
+ (HomePageModel *)AlarmAssist;
//協(xié)查派發(fā)
+ (HomePageModel *)AlarmDistribute;
//安防巡邏
+ (HomePageModel *)Patrol;
//巡防督查
+ (HomePageModel *)PatrolCheck;

//簽到打卡
+ (HomePageModel *)ClockIn;
//新聞資訊
+ (HomePageModel *)News;
//掃一掃
+ (HomePageModel *)Scan;
//排行榜
+ (HomePageModel *)RankingList;
//人員管理
+ (HomePageModel *)StaffManage;
//志愿者審核
+ (HomePageModel *)VolunteerCheck;

//活動(dòng)報(bào)名
+ (HomePageModel *)Actity;
//志愿者申請(qǐng)
+ (HomePageModel *)Volunteer;

//信息查詢
+ (HomePageModel *)InfoSearch;
//信息發(fā)布
+ (HomePageModel *)InfoDistribute;

//安全小知識(shí)
+ (HomePageModel *)SafeKnowledge;
//學(xué)習(xí)培訓(xùn)
+ (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

//警情上報(bào)
+ (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];
}
//協(xié)查協(xié)辦
+ (HomePageModel *)AlarmAssist{
    return [HomePageModel addModelWithName:@"title_assist" ImageName:@"icon_assist" Code:HomePage_AlarmAssist];
}
//協(xié)查派發(fā)
+ (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];
}

//活動(dòng)報(bào)名
+ (HomePageModel *)Actity{
    return [HomePageModel addModelWithName:@"title_activity" ImageName:@"icon_volunteer_check" Code:HomePage_Actity];
}
//志愿者申請(qǐng)
+ (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];
}
//信息發(fā)布
+ (HomePageModel *)InfoDistribute{
    return [HomePageModel addModelWithName:@"title_infoDistribute" ImageName:@"icon_info_distribute" Code:HomePage_InfoDistribute];
}

//安全小知識(shí)
+ (HomePageModel *)SafeKnowledge{
    return [HomePageModel addModelWithName:@"title_safeKnowledge" ImageName:@"icon_safe_knowledge" Code:HomePage_SafeKnowledge];
}
//學(xué)習(xí)培訓(xùn)
+ (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);//這是個(gè)類似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,//物業(yè)人員
    UserRole_ProManage = 3,//物管人員
    UserRole_Police = 4,//民警
    UserRole_PoliceManage = 5,//領(lǐng)導(dǎo)

};

@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]];//警情上報(bào)
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//協(xié)查協(xié)辦
        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]];//活動(dòng)報(bào)名
        [arrayStudy addObject:[HomePageModel Volunteer]];//志愿者申請(qǐng)
        [arrayStudy addObject:[HomePageModel SafeKnowledge]];//安全小知識(shí)
        HomePageGroup *groupStudy = [HomePageGroup addGroupWithName:@"Home_Activity" Array:arrayStudy];
        [menuArray addObjectsFromArray:@[groupDayWork, groupCommon, groupStudy]];

    }else if (role == UserRole_Propery){
        //物業(yè)巡邏員
        NSMutableArray<HomePageModel> *arrayDayWork = [NSMutableArray<HomePageModel> array];
        [arrayDayWork addObject:[HomePageModel Patrol]];//安保巡邏
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上報(bào)
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//協(xié)查協(xié)辦
        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]];//安全小知識(shí)
        [arrayStudy addObject:[HomePageModel Study]];//學(xué)習(xí)培訓(xùn)
        HomePageGroup *groupStudy = [HomePageGroup addGroupWithName:@"Home_Study" Array:arrayStudy];
        
        [menuArray addObjectsFromArray:@[groupDayWork, groupCommon, groupStudy]];
        
    }else if (role == UserRole_ProManage){
        //物業(yè)管理
        NSMutableArray<HomePageModel> *arrayDayWork = [NSMutableArray<HomePageModel> array];
        [arrayDayWork addObject:[HomePageModel Patrol]];//安保巡邏
        [arrayDayWork addObject:[HomePageModel AlarmUpload]];//警情上報(bào)
        [arrayDayWork addObject:[HomePageModel AlarmAssist]];//協(xié)查協(xié)辦
        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]];//信息發(fā)布
        HomePageGroup *groupInfo = [HomePageGroup addGroupWithName:@"Home_Info" Array:arrayInfo];
        
        NSMutableArray<HomePageModel> *arrayStudy = [NSMutableArray<HomePageModel> array];
        [arrayStudy addObject:[HomePageModel SafeKnowledge]];//安全小知識(shí)
        [arrayStudy addObject:[HomePageModel Study]];//學(xué)習(xí)培訓(xùn)
        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]];//協(xié)查派發(fā)
        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]];//信息發(fā)布
        HomePageGroup *groupInfo = [HomePageGroup addGroupWithName:@"Home_Info" Array:arrayInfo];
        
        [menuArray addObjectsFromArray:@[groupDayWork, groupCommon, groupInfo]];
        
    }else if (role == UserRole_PoliceManage){
        //局/政法委領(lǐng)導(dǎo)
        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]];//信息發(fā)布
        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
//返回分區(qū)個(gè)數(shù)
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

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

//返回每個(gè)分區(qū)的item個(gè)數(shù)
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    HomePageGroup *group = self.dataSource[section];
    return group.array.count;
    
    //return 3;
}

//返回每個(gè)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;
    
}

//點(diǎn)擊事件
- (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:@"敬請(qǐng)期待"] animated:NO];
            break;
    }
}

這樣就可以了荚醒,主頁的代碼看起來簡(jiǎn)單多了。其它的工作都放到菜單的配置項(xiàng)里面隆嗅〗绺螅可讀性也更好了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末胖喳,一起剝皮案震驚了整個(gè)濱河市泡躯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌丽焊,老刑警劉巖较剃,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異技健,居然都是意外死亡写穴,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門雌贱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來啊送,“玉大人偿短,你說我怎么就攤上這事〔雒唬” “怎么了昔逗?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)篷朵。 經(jīng)常有香客問我纤子,道長(zhǎng),這世上最難降的妖魔是什么款票? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任控硼,我火速辦了婚禮,結(jié)果婚禮上艾少,老公的妹妹穿的比我還像新娘卡乾。我一直安慰自己,他們只是感情好缚够,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布幔妨。 她就那樣靜靜地躺著,像睡著了一般谍椅。 火紅的嫁衣襯著肌膚如雪误堡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天雏吭,我揣著相機(jī)與錄音锁施,去河邊找鬼。 笑死杖们,一個(gè)胖子當(dāng)著我的面吹牛悉抵,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播摘完,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼姥饰,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了孝治?” 一聲冷哼從身側(cè)響起列粪,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎谈飒,沒想到半個(gè)月后岂座,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡步绸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年掺逼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瓤介。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡吕喘,死狀恐怖赘那,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情氯质,我是刑警寧澤募舟,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站闻察,受9級(jí)特大地震影響拱礁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辕漂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一呢灶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧钉嘹,春花似錦鸯乃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至陈辱,卻和暖如春奖年,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背沛贪。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工陋守, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鹏浅。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓易猫,卻偏偏與公主長(zhǎng)得像稠腊,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吏颖,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354