現在許多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;
}
}
這樣就可以了,主頁的代碼看起來簡單多了。其它的工作都放到菜單的配置項里面。可讀性也更好了。