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

現(xiàn)在許多App的首頁(yè)都是采用的九宮格形式,一級(jí)菜單(標(biāo)題)下面跟二級(jí)菜單(圖標(biāo)+標(biāo)題)。對(duì)于這樣一個(gè)頁(yè)面來(lái)說(shuō),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)入首頁(yè)有五個(gè)角色,每個(gè)角色的菜單都會(huì)不同。如果只有細(xì)微的差別的話,我會(huì)選擇在Delegate里面添加一些判斷。但是我接到的設(shè)計(jì)方案五個(gè)角色菜單的差距還是比較大的。如果要硬寫(xiě)判斷可能會(huì)有比較大的代碼量,且復(fù)雜易出錯(cuò)。對(duì)閱讀代碼也會(huì)不太友好。所以我想了別的辦法控制。

想法一:

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

想法二:

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

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

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

homePageModel里面用枚舉定義一個(gè)HomePageCode。通過(guò)code來(lái)判斷執(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ù)組來(lái)存放一級(jí)菜單,數(shù)組的count就是一級(jí)菜單的數(shù)量。
  • 想法二:然后用一個(gè)字典來(lái)保存每個(gè)角色的主頁(yè)菜單,key<NSString>代表一級(jí)菜單, value <NSArray>表示二級(jí)菜單。然后就可以判斷字段中key的數(shù)量來(lái)表示一級(jí)菜單的數(shù)量,內(nèi)容表示一級(jí)菜單的title。通過(guò)rows取到二級(jí)菜單的詳情。

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

代碼如下:

//  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

主頁(yè)

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

這樣就可以了,主頁(yè)的代碼看起來(lái)簡(jiǎn)單多了。其它的工作都放到菜單的配置項(xiàng)里面??勺x性也更好了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容