前言
這個控件比較常用,至少目前公司三個項目都用到了這個下拉菜單,是時候封裝分享一下了。覺的不錯的麻煩點(diǎn)個喜歡,三克油。控件布局使用的有名的第三方約束工具M(jìn)asonry,如果項目中已經(jīng)導(dǎo)入的話就不要再次導(dǎo)入了,不然會報錯。
偷偷告訴你,美團(tuán)的有個bug,單身20+以上的手速都可以發(fā)現(xiàn),沒發(fā)現(xiàn)說明你是在掩飾
注:Masonry配置,一般在pch文件
// 定義這個常量,就可以不用在開發(fā)過程中使用"mas_"前綴。
#define MAS_SHORTHAND
// 定義這個常量,就可以讓Masonry幫我們自動把基礎(chǔ)數(shù)據(jù)類型的數(shù)據(jù),自動裝箱為對象類型。
#define MAS_SHORTHAND_GLOBALS
#import <Masonry.h>
demo效果:

1.創(chuàng)建下拉菜單并設(shè)置代理
menuView = [PullDownMenuView new];
menuView.dataSource = self;
[self.view addSubview:menuView];
[menuView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view);
make.height.mas_equalTo(44);
make.top.mas_equalTo(self.view).offset(self.navigationController.navigationBar.frame.size.height+20);
}];
2.添加所有下拉菜單對應(yīng)的子控制器
為什么要這樣設(shè)計? 因?yàn)槊總€app對應(yīng)的下拉菜單不確定,所以交給各個開 發(fā)者決定,下拉菜單的界面。
- (NSArray *)viewControlls {
if (!_viewControlls) {
_viewControlls = @[[NSClassFromString(@"TestViewController1") new],
[NSClassFromString(@"TestViewController2") new],
[NSClassFromString(@"TestViewController3") new],
[NSClassFromString(@"TestViewController2") new]];
}
return _viewControlls;
}
- (void)setUPChildVC {
for (UIViewController *vc in self.viewControlls) {
[self addChildViewController:vc];
}
}
3.實(shí)現(xiàn)PullDownMenu數(shù)據(jù)源方法
#pragma mark - PullDownMenuDataSource
// 返回下拉菜單多少列
- (NSInteger)numberOfColsInMenu:(PullDownMenuView *)pullDownMenu {
return self.viewControlls.count;
}
// 返回下拉菜單每列按鈕標(biāo)題
- (NSString *)pullDownMenu:(PullDownMenuView *)pullDownMenu titleForColAtIndex:(NSInteger)index {
return [self.titleArr objectAtIndex:index];
}
// 返回下拉菜單每列對應(yīng)的控制器
- (UIViewController *)pullDownMenu:(PullDownMenuView *)pullDownMenu viewControllerForColAtIndex:(NSInteger)index {
return [self.viewControlls objectAtIndex:index];
}
// 返回下拉菜單每列對應(yīng)的高度
- (CGFloat)pullDownMenu:(PullDownMenuView *)pullDownMenu heightForColAtIndex:(NSInteger)index {
if (index == 0) {
return 233;
}
else if (index == 1){
return 200;
}
else if (index == 2){
return 44*4;
}
return 250;
}
- (NSArray *)titleArr {
if (!_titleArr) {
_titleArr = @[@"全部",
@"附近",
@"智能排序",
@"刷選"];
}
return _titleArr;
}
4.【更新菜單標(biāo)題,需要發(fā)送通知給我】
為什么要這樣設(shè)計?解耦,自己的控制器中就不需要導(dǎo)入我的框架的頭文件了,侵入性不大。
【更新菜單標(biāo)題步驟】
1.把 【extern NSString * const UpdateMenuTitleNote;】這行代碼拷貝到自己控制器中,這個在PullDownMenuView.h中
2.在選中標(biāo)題的方法中,發(fā)送以下通知
[[NSNotificationCenter defaultCenter] postNotificationName:UpdateMenuTitleNote object:self userInfo:@{@"title":cell.textLabel.text}];
3.1 postNotificationName:通知名稱 =>【UpdateMenuTitleNote】
3.2 object:誰發(fā)送的通知 =>【self】(當(dāng)前控制器)
3.3 userInfo:選中標(biāo)題信息 => 可以多個key,多個value,沒有固定的,因?yàn)橛行┙缑?,需要勾選很多選項,key可以隨意定義。
3.4 底層會自動判定,當(dāng)前userInfo有多少個value,如果有一個就會直接更新菜單標(biāo)題,有多個就會更新,滿足大部分需求。
3.5 發(fā)出通知,會自動彈回下拉菜單
可以參考TestViewController3中代碼
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (selectIndex == indexPath.row) {
}
else{
NSInteger oldIndex = selectIndex;
selectIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:oldIndex inSection:0],indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:UpdateMenuTitleNote object:self userInfo:@{@"title":[self.array objectAtIndex:selectIndex]}];
});
}