技术文章分类(180)

技术随笔(11)

仿美团下拉菜单DropDownMenu

github 下载demo:https://github.com/MartinLi841538513/MartinDemos (一切以demo为准)

 

1,下载好dropDownList库,拖到自己项目中。

2,在.h文件中引入

#import <UIKit/UIKit.h>
#import "DropDownListView.h"
#import "DropDownChooseProtocol.h"

@interface DropDownDemoViewController : UIViewController<DropDownChooseDelegate,DropDownChooseDataSource>
@property (weak, nonatomic) IBOutlet DropDownListView *dropDownView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *dropDownViewHeight;
@property(nonatomic,strong)NSArray *chooseArray;
@end

这里的dropDownView和dropDownViewHeight对应xib文件中得view和height,我都用箭头给你画好了。

chooseArray是数据源,在.m文件中会赋值。

 

3,在.m文件中赋值,以及设置dropDownView。这里注意,我是事先改过dropdownlist源代码的,我觉得我这样设计更合理,且更方便。而且可以结合autolayout使用,很爽。

#import "DropDownDemoViewController.h"

@interface DropDownDemoViewController ()

@end

@implementation DropDownDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.chooseArray = [NSMutableArray arrayWithArray:@[
                                                   @[@"童明城",@"童赟",@"童林杰",@"老萧狗"],
                                                   @[@"郏童熙",@"胥童嘉",@"郑嘉琦"]
                                                   ]];
    self.dropDownView.dropDownDataSource = self;
    self.dropDownView.dropDownDelegate = self;
    self.dropDownView.height = self.dropDownViewHeight.constant;
    [self.dropDownView setView];
    self.dropDownView.mSuperView = self.view;
}


#pragma mark -- dropDownListDelegate
-(void) chooseAtSection:(NSInteger)section index:(NSInteger)index
{
    NSLog(@"童大爷选了section:%d ,index:%d",section,index);
}

#pragma mark -- dropdownList DataSource
-(NSInteger)numberOfSections
{
    return [self.chooseArray count];
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSArray *arry =self.chooseArray[section];
    return [arry count];
}
-(NSString *)titleInSection:(NSInteger)section index:(NSInteger) index
{
    return self.chooseArray[section][index];
}
-(NSInteger)defaultShowSection:(NSInteger)section
{
    return 0;
}
@end

 

 

这里是另外一个,有三级目录,效果更好,更新于2015年3月1日。

http://code.cocoachina.com/detail/284158/%E7%B1%BB%E4%BC%BC%E7%BE%8E%E5%9B%A2%E7%9A%84%E4%B8%8B%E6%8B%89%E8%8F%9C%E5%8D%95/

posted @ 2014-12-15 21:36  坤哥MartinLi  阅读(1603)  评论(0编辑  收藏  举报