CMDropDownMenu
- github地址:<a >https://github.com/smallyou/CMDropDownMenu</a>
- A simple and convenient drop-down menu framework
- 一個(gè)簡單、方便的下拉菜單框架
- Non intrusive, word integration
- 無侵入性,一句話集成
GitHub: <a >smallyou</a> | 簡書: <a href = "http://www.itdecent.cn/u/ebb60643b57c">一月二十三</a> | 微信公眾賬號(hào):<a >一路上有你</a>

CMDropDownMenu.gif
1 Started(開始使用)
1.1 Feature(特點(diǎn))
- 使用簡單,直接init即可
- 數(shù)據(jù)源為數(shù)組,直接賦值即可
- 多種類型的數(shù)據(jù)源(plist/json/手動(dòng)創(chuàng)建)
1.2 Install(安裝)
1.2.1 cocoapods
暫無不支持, 后續(xù)擴(kuò)充....
1.2.2 手動(dòng)安裝
- 將項(xiàng)目中的 <a>CMDropDownMenu</a> 文件夾拖入到工程中
- 引入頭文件 <a>#import "CMDropDownMenu.h"</a>
2 Create DataSource(創(chuàng)建數(shù)據(jù)源)
2.1 手動(dòng)創(chuàng)建數(shù)據(jù)源
CMDropMenuItem *item1 = [CMDropMenuItem itemWithSuperItem:nil Id:0 title:@"全部居委" subItems:nil];
CMDropMenuItem *item11 = [CMDropMenuItem itemWithSuperItem:item1 Id:0 title:@"全部居委" subItems:nil];
item11.isDefaultItem = YES; //設(shè)置默認(rèn)選項(xiàng),當(dāng)點(diǎn)擊當(dāng)前選項(xiàng),菜單欄不會(huì)變化
CMDropMenuItem *item12 = [CMDropMenuItem itemWithSuperItem:item1 Id:0 title:@"楓葉" subItems:nil];
CMDropMenuItem *item13 = [CMDropMenuItem itemWithSuperItem:item1 Id:0 title:@"廣氮社區(qū)" subItems:nil];
CMDropMenuItem *item14 = [CMDropMenuItem itemWithSuperItem:item1 Id:0 title:@"廣州景聯(lián)科技" subItems:nil];
CMDropMenuItem *item15 = [CMDropMenuItem itemWithSuperItem:item1 Id:0 title:@"荷光東" subItems:nil];
item1.subItems = @[item11,item12,item13,item14,item15];
CMDropMenuItem *item2 = [CMDropMenuItem itemWithSuperItem:nil Id:0 title:@"家庭醫(yī)生" subItems:nil];
CMDropMenuItem *item21 = [CMDropMenuItem itemWithSuperItem:item2 Id:0 title:@"不限" subItems:nil];
item21.isDefaultItem = YES; //設(shè)置默認(rèn)選項(xiàng),當(dāng)點(diǎn)擊當(dāng)前選項(xiàng),菜單欄不會(huì)變化
CMDropMenuItem *item22 = [CMDropMenuItem itemWithSuperItem:item2 Id:0 title:@"已簽約" subItems:nil];
CMDropMenuItem *item23 = [CMDropMenuItem itemWithSuperItem:item2 Id:0 title:@"未簽約" subItems:nil];
item2.subItems = @[item21,item22,item23];
NSArray *dataTitles = @[item1, item2];
2.2 標(biāo)準(zhǔn)格式的plist文件創(chuàng)建
2.2.1 plist格式
- plist文件的root類型是NSArray,內(nèi)部item是NSDictionary類型。
- 字典的key一般為<a>CMDropMenuItem</a>的屬性名,如果屬性名不對(duì)應(yīng),可以使用類方法<a>cm_setupReplacedKeyFromPropertyName:</a>來聲明。
- 如果字典內(nèi)部有NSArray屬性,屬性內(nèi)部又包含一個(gè)模型,則可以使用類方法<a>cm_setupObjectClassInArray:</a>來聲明,框架自動(dòng)轉(zhuǎn)換成模型數(shù)組。
【plist格式截圖】

標(biāo)準(zhǔn)的plist.png
2.2.2 加載方式
// 通過標(biāo)準(zhǔn)的plist文件創(chuàng)建數(shù)據(jù)源,返回?cái)?shù)據(jù)源數(shù)組
- (NSArray *)setupDataSourceWithStandardFormatPlist
{
[CMDropMenuItem cm_setupReplacedKeyFromPropertyName:@{
@"subItem":@"subItems"
}];
[CMDropMenuItem cm_setupObjectClassInArray:@{
@"subItem":@"CMDropMenuItem"
}];
return [CMDropMenuItem itemsWithContentsOfStandardFormatFile:[[NSBundle mainBundle] pathForResource:@"menu1.plist" ofType:nil]];
}
2.3 簡單格式的plist文件創(chuàng)建
2.3.1 plist的格式
- plist的root類型是NSArray類型,數(shù)組中包含了字典。字典的key即為一級(jí)菜單。
- 字典的value為字符串?dāng)?shù)組時(shí),數(shù)組中的字符串即為二級(jí)菜單
- 字典的value為字典數(shù)組時(shí),表示有三級(jí)菜單。字典數(shù)組中的key為二級(jí)菜單,value為三級(jí)菜單
【簡單plist格式截圖】

簡單的plist.png
2.3.2 加載方式
// 通過簡單的plist文件創(chuàng)建
- (NSArray *)setupDataSourceWithSampleFormatPlist
{
return [CMDropMenuItem itemsWithContentsOfSampleFormatFile:[[NSBundle mainBundle] pathForResource:@"menu2.plist" ofType:nil]];
}
2.4 簡單格式的json文件創(chuàng)建
2.4.1 json文件格式
-
格式與簡單的plist類似
簡單的json.png
2.4.2 加載方式
//通過簡單的json文件創(chuàng)建數(shù)據(jù)源,返回?cái)?shù)據(jù)源數(shù)組
- (NSArray *)setupDataSourceWithSampleFormatJson
{
//讀取數(shù)據(jù)
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"menu2.json" ofType:nil]];
//轉(zhuǎn)換成NSArray
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//返回
return [CMDropMenuItem itemsWithSampleFormatKeyValueArray:array];
}
2.5 其他API
- 可以設(shè)置菜單欄是否滾動(dòng),默認(rèn)不可滾動(dòng)
- 可以設(shè)置菜單欄的字體及顏色
- 可以設(shè)置一級(jí)子菜單的字體及顏色
- 可以設(shè)置二級(jí)子菜單的字體及顏色
- 可以設(shè)置數(shù)據(jù)源數(shù)組中的字典數(shù)組所盛放的模型

其他API.png
3 Useage(使用)
3.1 加載view
- (void)viewDidLoad
{
[super viewDidLoad];
//CMDropDownMenu *dropDownMenu = [[CMDropDownMenu alloc]initWithMenuBarScrollable:YES]; //設(shè)置菜單欄可以滾動(dòng)
CMDropDownMenu *dropDownMenu = [[CMDropDownMenu alloc]init]; //默認(rèn)是NO 不可滾動(dòng)
self.dropDownMenu = dropDownMenu;
//可選屬性配置
dropDownMenu.menuTitleColor= [UIColor blackColor]; //在賦數(shù)據(jù)源之前設(shè)置,如果為空或不傳,則默認(rèn)是黑色的
dropDownMenu.menuTitleFont = [UIFont systemFontOfSize:16.0]; //在賦數(shù)據(jù)源之前設(shè)置,如果為空或不傳,則默認(rèn)是16號(hào)字體
dropDownMenu.firstMenuTitleColor = [UIColor blackColor]; //在賦數(shù)據(jù)源之前設(shè)置,如果為空或不傳,則默認(rèn)是黑色的
dropDownMenu.firstMenuTitleFont = [UIFont systemFontOfSize:13.0]; //在賦數(shù)據(jù)源之前設(shè)置,如果為空或不傳,則默認(rèn)是13號(hào)字體
//數(shù)據(jù)源配置
dropDownMenu.titleDatas = [self setupDataSourceWithSampleFormatJson]; //通過簡單格式的json創(chuàng)建
//dropDownMenu.titleDatas = [self setupDataSourceWithSampleFormatPlist]; //通過簡格式的plist創(chuàng)建
//dropDownMenu.titleDatas = [self setupDataSourceWithStandardFormatPlist]; //通過標(biāo)準(zhǔn)格式的plist創(chuàng)建
//dropDownMenu.titleDatas = [self setupDataSourceIgnoreSuperItem]; //通過手動(dòng)創(chuàng)建忽略superItem
dropDownMenu.delegate = self;
[self.view addSubview:dropDownMenu];
self.title = @"下拉列表菜單";
}
3.2 獲取選中路徑
- 代理返回只是最終選中的葉子節(jié)點(diǎn)的CMDropMenuItem模型,可以通過模型的superItem屬性反向獲取整體的選中路徑
#pragma mark - CMDropDownMenuDelegate
// 選中葉子節(jié)點(diǎn)后的回調(diào)
- (void)dropDownMenu:(CMDropDownMenu *)dropDownMenu didChildItemSelected:(CMDropMenuItem *)item
{
NSString *path = @"";
path = [@"選中的路徑是:" stringByAppendingPathComponent:[self getChoosePath:item]];
self.resultLabel.text = path;
}
/**反向獲取選擇路徑*/
- (NSString *)getChoosePath:(CMDropMenuItem *)item
{
NSString *result = item.title;
//獲取父節(jié)點(diǎn)
if (item.superItem) {
CMDropMenuItem *superItem = item.superItem;
//拼接
return [[self getChoosePath:superItem] stringByAppendingPathComponent:result];
}else{
return result;
}
}
