iOS - 自定義下拉菜單

效果圖:

E4AED2F4-F66C-466B-9144-8943393A954A.png
9C977A4C-2648-4AAC-925D-2FB09B8FD361.png

.h文件里面要實(shí)現(xiàn)的協(xié)議的方法

在YDDropdownMenuDelegate里面要實(shí)現(xiàn)的協(xié)議方法有以下幾種:
/** 
* net代理方法返回 菜單下標(biāo):index 菜單標(biāo)題:MenuTitle
*/
- (void)netClickMenuIndex:(NSInteger)menuIndex menuTitle:(NSString *)menuTitle;

/** 
* 代理方法返回 菜單下標(biāo):index  一級(jí)菜單index:firstIndex  一級(jí)菜單名 
*/
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex firstIndex:(NSInteger)firstIndex andfirstIndexName:(NSString *)firstIndexName;

/** 
 * 代理方法返回 菜單下標(biāo):index  二級(jí)菜單index:firstIndex  二級(jí)菜單名 
 */
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex secondIndex:(NSInteger)secondIndex andsecondIndexName:(NSString *)secondIndexName;

/**
  *代理方法返回 菜單下標(biāo):index  三級(jí)菜單index:firstIndex  三級(jí)菜單名
 */
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex thirdIndex:(NSInteger)thirdIndex andthirdIndexName:(NSString *)thirdIndexName;

/** 
 * 代理方法返回,選中的category篩選產(chǎn)品 
 */
-(void)menuCellDidSelected:(NSInteger)MenuTitleIndex andCategoryName:(NSString *)categoryName;

/** 
 * 代理方法返回计螺,將本身視圖顯示在最上層 
 */    
-(void)bringselfViewToFront;

/** 
 * 代理方法返回珊搀,將本身視圖顯示在最collectionview下層 
 */
-(void)bringselfViewToBackCollection;
定義的一些常用屬性(可在調(diào)用時(shí)設(shè)置冶忱,如若不設(shè)置,則會(huì)調(diào)用默認(rèn)的值):
/** 遮蓋層顏色 */
@property (nonatomic, strong) UIColor * CarverViewColor;

/** 自定義tag值防止和頁面其他tag有沖突 */
@property (nonatomic, assign) NSInteger menuButtonTag;    

/** 遮蓋的動(dòng)畫時(shí)間 */
@property (nonatomic, assign) CGFloat   caverAnimationTime;

/** 縮進(jìn)的動(dòng)畫時(shí)間 */
@property (nonatomic, assign) CGFloat   hideAnimationTime;

/** 菜單title的字體大小 */
@property (nonatomic, assign) CGFloat   menuTitleFont;

/** 下拉菜單的的字體大小 */
@property (nonatomic, assign) CGFloat   tableTitleFont;

/** 菜單的的高度 */
@property (nonatomic, assign) CGFloat   menuHeight;

/** 下拉菜單cell的高度 */
@property (nonatomic, assign) CGFloat   cellHeight;

/** 下拉的Tableview最大高度(超出高度可以滑動(dòng)顯示) */
@property (nonatomic, assign) CGFloat   tableViewMaxHeight;

/** 未選中字體的顏色 */
@property (nonatomic, strong) UIColor  * unSelectedColor;

/** 選中字體的顏色 */
@property (nonatomic, strong) UIColor  * selectedColor;

/** 選中背景圖片 */
@property (nonatomic, strong) UIImage  * selectedImage;

/** 設(shè)置代理 */
@property (nonatomic,weak) id <YDDropdownMenuDelegate> delegate;

@property (nonatomic,strong) UITableView    * tableFirst;
@property (nonatomic,strong) UITableView    * tableSecond;
@property (nonatomic,strong) UITableView    * tableThird;
在初始化各個(gè)下拉的tableview時(shí)調(diào)用以下的方法進(jìn)行創(chuàng)建:
#pragma mark -- net一級(jí)一級(jí)點(diǎn)擊網(wǎng)絡(luò)獲取數(shù)據(jù)創(chuàng)建

/** net創(chuàng)建下拉菜單 */
- (void)netCreateMenuTitleArray:(NSArray *)menuTitleArray;

/** net導(dǎo)入一級(jí)菜單數(shù)據(jù) */
- (void)netLoadFirstArray:(NSArray *)firstArray andMenuIndex:    (NSInteger)menuIndex andArrow:(BOOL)arrow;

/** net導(dǎo)入二級(jí)菜單數(shù)據(jù) */
- (void)netLoadSecondArray:(NSArray *)SecondArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow;

/** net導(dǎo)入三級(jí)菜單數(shù)據(jù) */
- (void)netLoadThirdArray:(NSArray *)ThirdArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow;

-(void)reloadTableView:(UITableView *)tableView;

.m文件里面要實(shí)現(xiàn)的方法

宏定義的一些默認(rèn)值(在用戶初始化不設(shè)置時(shí)調(diào)用):
#define cell_h                      33
#define carverAnimationDefalutTime  0.15
#define hideAnimationDefalutTime    0.15
#define tableViewMaxHeightDefalut   198
#define menuButtonDefalutTag        700
#define triangleImageDefalutTag     100

#define menuTitleDefalutFont        [UIFont systemFontOfSize:13]
#define TableTitleDefalutFont       [UIFont systemFontOfSize:11]
#define SeparatorColor              YDColor(249, 249, 249)
#define Drop_triangle               @"Drop_triangle"
#define Pullup_triangle             @"Pullup_triangle"
定義的一些屬性:
@interface YDDropdownMenu ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIImageView    * triangleImageV;  //三角形圖標(biāo)
@property (nonatomic, strong) UIView         * buttonBackView;
@property (nonatomic, strong) UIButton       * typeBtn ;

@property (nonatomic, strong) NSMutableArray * dataSourceFirst;
@property (nonatomic, strong) NSMutableArray * dataSourceSecond;
@property (nonatomic, strong) NSMutableArray * dataSourceThird;

@property (nonatomic, assign) BOOL             firstTableViewShow;
@property (nonatomic, assign) BOOL             secondTableViewShow;
@property (nonatomic, assign) BOOL             thirdTableViewShow;

@property (nonatomic, assign) NSInteger        lastSelectedButtonIndex;
@property (nonatomic, assign) NSInteger        lastSelectedCellIndex;
@property (nonatomic, assign) NSInteger        lastSecondCellIndex;
@property (nonatomic, assign) NSInteger        num;

@property (nonatomic, assign) CGFloat          tableViewWith;
@property (nonatomic, assign) CGFloat          buttonBackWithSum;
@property (nonatomic, assign) CGFloat          menuBaseHeight;

@property (nonatomic, assign) BOOL             isNet;
@property (nonatomic, assign) BOOL             arrow;
@property (nonatomic, assign) BOOL             hasThirdTableView;

@end
設(shè)置數(shù)組懶加載:
-(NSMutableArray *)dataSourceFirst{

if (!_dataSourceFirst) {
    
    _dataSourceFirst = [[NSMutableArray alloc] init];
}

return _dataSourceFirst;
}

-(NSMutableArray *)dataSourceThird{

if (!_dataSourceThird) {
    _dataSourceThird = [[NSMutableArray alloc] init];
}
return _dataSourceThird;
}

-(NSMutableArray *)dataSourceSecond{

if (!_dataSourceSecond) {
    _dataSourceSecond = [[NSMutableArray alloc] init];
}
return _dataSourceSecond;
}
添加removeDropdownMenuNotifacation通知境析,在外部發(fā)送通知隱藏下拉菜單時(shí)接收通知囚枪,對(duì)tableview菜單進(jìn)行隱藏:
-(instancetype)initWithFrame:(CGRect)frame{

if (self = [super initWithFrame:frame]) {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeDropdownMenu:) name:removeDropdownMenuNotifacation object:nil];
}

return self;
}
創(chuàng)建下拉主菜單(循環(huán)創(chuàng)建需要的button個(gè)數(shù)):
#pragma mark -- net一級(jí)一級(jí)點(diǎn)擊網(wǎng)絡(luò)獲取數(shù)據(jù)創(chuàng)建
- (void)netCreateMenuTitleArray:(NSArray *)menuTitleArray{

self.isNet = YES;
self.menuBaseHeight = 18;
self.tableViewMaxHeight = self.cellHeight * 6;
[self createMenuViewWithData:menuTitleArray];
[self createTableViewFirst];
[self createTableViewSecond];
[self createTableViewThird];
}
創(chuàng)建每個(gè)button下可能需要的tableview菜單:
//創(chuàng)建第一級(jí)目錄的tableview 
- (void)netLoadFirstArray:(NSArray *)firstArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow{

self.arrow = arrow;

[self createWithFirstData:firstArray];

[self netShowFirstTableByMenuIndex:menuIndex];
 }

//創(chuàng)建第二級(jí)目錄的tableview 
- (void)netLoadSecondArray:(NSArray *)SecondArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow{

self.arrow = arrow;

if ( SecondArray.count > 0) {
    
    [self.dataSourceSecond removeAllObjects];
    [self.dataSourceSecond addObjectsFromArray:SecondArray];
}
if( arrow == YES ){
    
     self.hasThirdTableView = YES;
}
[self showSecondTabelView:self.secondTableViewShow MenuIndex:menuIndex];
}

//創(chuàng)建第三級(jí)目錄的tableview 
-(void)netLoadThirdArray:(NSArray *)ThirdArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow{

self.arrow = arrow;

if (ThirdArray.count > 0) {
    
    [self.dataSourceThird removeAllObjects];
    
    [self.dataSourceThird addObjectsFromArray:ThirdArray];
    
    [self showThirdTabelView:self.thirdTableViewShow MenuIndex:menuIndex];
    
    [self.tableThird reloadData];
}
else{
    
    UIButton *btn = (UIButton *)[self viewWithTag:self.lastSelectedButtonIndex];
    [btn setTitle:_dataSourceSecond[self.lastSelectedCellIndex] forState:UIControlStateNormal];
    btn.selected = YES;
    [self remover];
}
}
當(dāng)需要顯示某個(gè)tableview時(shí)進(jìn)行調(diào)用:
//顯示第一級(jí)目錄的tableview 
- (void)netShowFirstTableByMenuIndex:(NSInteger)menuIndex{

//    NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;
//    NSInteger index = self.lastSelectedButtonIndex;
CGFloat TableViewW = self.cellHeight*self.dataSourceFirst.count;
CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;

//判斷是否需要滑動(dòng)
if (TableViewW > MaxHeigth) {
    
    self.tableFirst.scrollEnabled = YES;
    
    TableViewW = MaxHeigth;
    
}else{
    
    self.tableFirst.scrollEnabled = NO;
}

if (self.firstTableViewShow == NO) {
    
    self.tableFirst.frame = CGRectMake(self.tableViewWith*(menuIndex - 700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    
    [UIView animateWithDuration:self.caverAnimationTime animations:^{
        
        self.tableFirst.frame = CGRectMake(self.tableViewWith*(menuIndex - 700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
        
    }completion:^(BOOL finished) {
    }];
    
}else{
    
    self.firstTableViewShow = NO;
    
    self.tableFirst.frame = CGRectMake(0, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
    [UIView animateWithDuration:self.hideAnimationTime animations:^{
        self.tableFirst.frame = CGRectMake(0, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    }];
}
}

//顯示第二級(jí)目錄的tableview 
- (void)showSecondTabelView:(BOOL)secondTableViewShow MenuIndex:(NSInteger)menuIndex{

CGFloat TableViewW = self.cellHeight*self.dataSourceSecond.count;
CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;

if (TableViewW > MaxHeigth) {
    self.tableSecond.scrollEnabled = YES;
    TableViewW = MaxHeigth;
    
}else{
    
    self.tableSecond.scrollEnabled = NO;
}

if (self.secondTableViewShow == YES) {
    
    self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    [UIView animateWithDuration:self.caverAnimationTime animations:^{
        self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
    }];
    
}else{

    self.secondTableViewShow = YES;
    self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    [UIView animateWithDuration:self.caverAnimationTime animations:^{
        self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame),self.tableViewWith, TableViewW);
    }];
}
[self.tableSecond reloadData];
}

//顯示第三級(jí)目錄的tableview 
- (void)showThirdTabelView:(BOOL)thirdTableViewShow MenuIndex:  (NSInteger)menuIndex{

CGFloat TableViewW = self.cellHeight*self.dataSourceThird.count;
CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;
if (TableViewW > MaxHeigth) {
    
    self.tableThird.scrollEnabled = YES;
    TableViewW = MaxHeigth;
    
}else{
    self.tableThird.scrollEnabled = NO;
}

if (self.thirdTableViewShow == YES) {
  
    self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    [UIView animateWithDuration:self.caverAnimationTime animations:^{
        self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
    }];
    
}else{
   
    self.thirdTableViewShow = YES;
   
    self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    [UIView animateWithDuration:self.caverAnimationTime animations:^{
        self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame),self.tableViewWith, TableViewW);
    }];
}

}
當(dāng)需要隱藏tableview時(shí)進(jìn)行調(diào)用:
- (void)hideCarverView{

self.firstTableViewShow = NO;
self.secondTableViewShow = NO;
self.thirdTableViewShow = NO;

if (_delegate && [_delegate respondsToSelector:@selector(bringselfViewToBackCollection)]) {
    
    [_delegate bringselfViewToBackCollection];
}

[UIView animateWithDuration:self.hideAnimationTime animations:^{
    
    self.tableFirst.frame = CGRectMake(self.tableViewWith*(_lastSelectedButtonIndex-700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    
    self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    
    self.tableThird.frame = CGRectMake(self.tableViewWith * 2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    
    self.backgroundColor = [UIColor clearColor];
    
}];

NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;

for (int i =0; i < 3;i++) {

    UIButton * button = (id)[self viewWithTag:tempTag + i];
    button.selected = YES;
    [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
}

}
給tableview賦值:
#pragma mark -- 給tableview賦值
- (void)createWithFirstData:(NSArray *)dataFirst{

self.dataSourceFirst = [NSMutableArray arrayWithArray:dataFirst];

[self.tableFirst reloadData];
}

- (void)createWithSecondData:(NSArray *)dataSecond{

self.dataSourceSecond = [NSMutableArray arrayWithArray:dataSecond];

[self.tableSecond reloadData];
}

- (void)createWithThirdData:(NSArray *)dataThird{

self.dataSourceThird = [NSMutableArray arrayWithArray:dataThird];

[self.tableThird reloadData];
}
創(chuàng)建分類按鈕:
#pragma mark -- 創(chuàng)建分類按鈕
-(void)createMenuViewWithData:(NSArray *)menuTitleArray{

self.buttonBackWithSum = 240.0;
self.tableViewWith = self.buttonBackWithSum*0.3333;
self.backgroundColor = [UIColor clearColor];

self.caverAnimationTime = self.caverAnimationTime ? self.caverAnimationTime : carverAnimationDefalutTime;
self.hideAnimationTime = self.hideAnimationTime ? self.hideAnimationTime : hideAnimationDefalutTime;
self.cellHeight = self.cellHeight ? self.cellHeight : cell_h;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remover)];
[self addGestureRecognizer:tap];
tap.delegate = self;


self.buttonBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 33)];
self.buttonBackView.userInteractionEnabled = YES;
self.buttonBackView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.buttonBackView];

UIImageView * backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 35)];
backImage.image = [UIImage imageNamed:@"typeSelected_backImage"];
[self.buttonBackView addSubview: backImage];

NSInteger num = menuTitleArray.count;
//    CGFloat btnW = (self.buttonBackWithSum-num+1)/num;
for (int i = 0; i < num; i++) {
    
    //(btnW+1)*i + 16
    _typeBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.tableViewWith *i+ 16, 8, 55, self.menuBaseHeight)];
    NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;
    _typeBtn.tag = tempTag+i;
    _typeBtn.layer.cornerRadius = 5;
    _typeBtn.selected = YES;
    
    _typeBtn.titleLabel.font = self.menuTitleFont ? [UIFont systemFontOfSize:self.menuTitleFont] : menuTitleDefalutFont;
    
    _typeBtn.backgroundColor = [UIColor whiteColor];

    _typeBtn.adjustsImageWhenHighlighted = NO;
    
    [_typeBtn setTitle: menuTitleArray[i] forState:UIControlStateNormal];
    UIColor * titleColor = self.unSelectedColor?self.unSelectedColor :  [UIColor lightGrayColor];
    [_typeBtn setTitleColor:titleColor forState:UIControlStateNormal];

    _typeBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [_typeBtn addTarget:self action:@selector(showFirstTableViewByButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.buttonBackView addSubview:_typeBtn];
    
    //三角形
    _triangleImageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.tableViewWith * (i+1) - 17, CGRectGetMidY(_typeBtn.frame), 4.5, 2.5)];
    
    _triangleImageV.image = [UIImage imageNamed:Drop_triangle];
    _triangleImageV.tag = triangleImageDefalutTag + i;
    [self.buttonBackView addSubview:_triangleImageV];
}
}

//三角形顏色
-(void)DefalutTriangleImage{

for (int i = 0; i < 3; i++) {
    
    UIImageView * triangleImageV = (id)[self viewWithTag:triangleImageDefalutTag + i];
    
    triangleImageV.image = [UIImage imageNamed:Drop_triangle];
    
}
}
通過點(diǎn)擊某個(gè)button來顯示第一個(gè)tableview(第一次點(diǎn)擊menu上面的button):
- (void)showFirstTableViewByButton:(UIButton *)btn{

_lastSelectedButtonIndex = btn.tag;
[self hideCarverView];
[self DefalutTriangleImage];

if (btn.selected == YES) {
    
    NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;

    UIImageView * triangleImageV = (id)[self viewWithTag:triangleImageDefalutTag + (_lastSelectedButtonIndex -tempTag)];
    triangleImageV.image = [UIImage imageNamed:Pullup_triangle];
    
    if (_delegate && [_delegate respondsToSelector:@selector(bringselfViewToFront)]) {
        
        [_delegate bringselfViewToFront];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
       
        self.backgroundColor = YDColorAlp(12, 13, 28, 0.2);
    }];
    
    btn.selected = NO;
    
    if (_delegate && [_delegate respondsToSelector:@selector(netClickMenuIndex:menuTitle:)]) {
        
        [_delegate netClickMenuIndex:btn.tag menuTitle:btn.titleLabel.text];
    }
    
        for (UIButton * button in btn.superview.subviews) {
            
            if (button.tag != _lastSelectedButtonIndex && [button isKindOfClass: [UIButton class]]) {
               
                button.selected = YES;
                [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            }
        }
    [btn setBackgroundImage:self.selectedImage forState:UIControlStateNormal];

}else{
    
    if (_delegate && [_delegate respondsToSelector:@selector(bringselfViewToBackCollection)]) {
        
        [_delegate bringselfViewToBackCollection];
    }
    self.firstTableViewShow = NO;
    btn.selected = YES;
    
    [UIView animateWithDuration:self.hideAnimationTime animations:^{
        
        self.backgroundColor = [UIColor clearColor];
        
        self.tableFirst.frame = CGRectMake(self.tableViewWith*(_lastSelectedButtonIndex-700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
        
        self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
        
        self.tableThird.frame = CGRectMake(self.tableViewWith * 2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
    }];

}
}
真正實(shí)現(xiàn)創(chuàng)建tableview的方法(上面只是在調(diào)用此方法):
#pragma mark -- 創(chuàng)建tableview
//創(chuàng)建第一級(jí)目錄的tableview的實(shí)現(xiàn)方法
- (void)createTableViewFirst{

self.tableFirst = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.buttonBackView.frame),self.buttonBackWithSum, 0) style:UITableViewStylePlain];
self.tableFirst.scrollEnabled = NO;
self.tableFirst.delegate = self;
self.tableFirst.dataSource = self;
[self.tableFirst setSeparatorColor:SeparatorColor];

self.firstTableViewShow = NO;

[self.tableFirst registerNib:[UINib nibWithNibName:product_TypeIdentifier bundle:nil] forCellReuseIdentifier:product_TypeIdentifier];

[self insertSubview:self.tableFirst belowSubview:self.buttonBackView];

}

//創(chuàng)建第二級(jí)目錄的tableview的實(shí)現(xiàn)方法
- (void)createTableViewSecond{

self.tableSecond = [[UITableView alloc]initWithFrame:CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0) style:UITableViewStylePlain];
self.tableSecond.scrollEnabled = NO;
self.tableSecond.delegate = self;
self.tableSecond.dataSource = self;
self.tableSecond.autoresizesSubviews = NO;
[self.tableSecond setSeparatorColor:SeparatorColor];

self.secondTableViewShow = NO;

[self.tableSecond registerNib:[UINib nibWithNibName:product_TypeIdentifier bundle:nil] forCellReuseIdentifier:product_TypeIdentifier];

[self insertSubview:self.tableSecond belowSubview:self.buttonBackView];
}

//創(chuàng)建第三級(jí)目錄的tableview的實(shí)現(xiàn)方法
- (void)createTableViewThird{

 self.hasThirdTableView = NO;

self.tableThird = [[UITableView alloc]initWithFrame:CGRectMake(self.tableViewWith*2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0) style:UITableViewStylePlain];
self.tableThird.scrollEnabled = NO;
self.tableThird.delegate = self;
self.tableThird.dataSource = self;
self.tableThird.autoresizesSubviews = NO;
[self.tableThird setSeparatorColor:SeparatorColor];

self.thirdTableViewShow = NO;

[self.tableThird registerNib:[UINib nibWithNibName:product_TypeIdentifier bundle:nil] forCellReuseIdentifier:product_TypeIdentifier];

[self insertSubview:self.tableThird belowSubview:self.buttonBackView];
}
tableview的代理實(shí)現(xiàn):
 #pragma mark -- tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return self.cellHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{

if (tableView == self.tableFirst) {
    
    return self.dataSourceFirst.count;
    
}else if (tableView == self.tableSecond) {
    
    return self.dataSourceSecond.count;
    
}else if (tableView == self.tableThird){
    
    return self.dataSourceThird.count;
}

return 0;
}

- (void)changeCellTextColorWith:(UITableViewCell *)cell{

cell.textLabel.textColor = self.unSelectedColor ?  self.unSelectedColor:[UIColor lightGrayColor];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

YDProduct_typeCell * cell = [tableView dequeueReusableCellWithIdentifier:product_TypeIdentifier forIndexPath:indexPath];
cell.cellTitleLabel.textColor = self.unSelectedColor ? self.unSelectedColor : [UIColor lightGrayColor];

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = YDColor(248, 248, 248);

if (self.arrow) {
    
    cell.cellArrowImage.hidden = NO;
}
else{
    
    cell.cellArrowImage.hidden = YES;
}

if (tableView == self.tableFirst) {

    cell.cellTitleLabel.text = self.dataSourceFirst[indexPath.row];
    cell.cellTitleLabel.font = self.tableTitleFont ? [UIFont systemFontOfSize:self.tableTitleFont] : TableTitleDefalutFont;
     
    [self changeCellTextColorWith:cell];
    
    return cell;
    
}else if (tableView == self.tableSecond){
   
    cell.cellTitleLabel.text = self.dataSourceSecond[indexPath.row];
    cell.cellTitleLabel.font = self.tableTitleFont ? [UIFont systemFontOfSize:self.tableTitleFont] : TableTitleDefalutFont;
    [self changeCellTextColorWith:cell];
    
    return cell;
    
}else if (tableView == self.tableThird){
   
    cell.cellTitleLabel.text = self.dataSourceThird[indexPath.row];
    //cell3.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cellTitleLabel.font = self.tableTitleFont ? [UIFont systemFontOfSize:self.tableTitleFont] : TableTitleDefalutFont;
    [self changeCellTextColorWith:cell];
    
    return cell;
}
return nil;
}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    NSLog(@"selectButtonTagIs %li arrow is %li \n indexPath = %li",_lastSelectedButtonIndex,self.arrow,indexPath.row);

self.lastSelectedCellIndex = indexPath.row;
UIButton *btn = (UIButton *)[self viewWithTag:self.lastSelectedButtonIndex];

if (tableView == self.tableFirst) {

     if (_lastSelectedButtonIndex == 700 && self.thirdTableViewShow == YES){
        
        self.thirdTableViewShow = NO;
        CGFloat TableViewW = self.cellHeight*self.dataSourceSecond.count;
        CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;
        
        //判斷是否需要滑動(dòng)
        if (TableViewW > MaxHeigth) {
            
            self.tableFirst.scrollEnabled = YES;
            
            TableViewW = MaxHeigth;
            
        }else{
            
            self.tableFirst.scrollEnabled = NO;
        }

        self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
        
        [UIView animateWithDuration:self.caverAnimationTime animations:^{
            
            self.tableThird.frame = CGRectMake(self.tableViewWith * 2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
            self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
        }];
         
         if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:firstIndex:andfirstIndexName:)]) {
             
             [_delegate menuCellDidSelected:_lastSelectedButtonIndex firstIndex:self.lastSelectedCellIndex andfirstIndexName:_dataSourceFirst[self.lastSelectedCellIndex]];
         }

     }else if (self.arrow == NO && self.secondTableViewShow == NO) {
         
         [btn setTitle:_dataSourceFirst[self.lastSelectedCellIndex] forState:UIControlStateNormal];
         btn.selected = YES;
         [self remover];
         NSLog(@"SelectFirst RowName is : %@",_dataSourceFirst[self.lastSelectedCellIndex]);
         
         if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:andCategoryName:)]) {
             
             [_delegate menuCellDidSelected:_lastSelectedButtonIndex andCategoryName:_dataSourceFirst[self.lastSelectedCellIndex]];
         }
         
         
     }else{
        
        if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:firstIndex:andfirstIndexName:)]) {
            
            [_delegate menuCellDidSelected:_lastSelectedButtonIndex firstIndex:self.lastSelectedCellIndex andfirstIndexName:_dataSourceFirst[self.lastSelectedCellIndex]];
        }
        [self.tableSecond reloadData];
    }
    
}else if (tableView == self.tableSecond) {
    
    //如果第一級(jí)存在第二級(jí)菜單派诬,并且已經(jīng)有了第三級(jí)菜單||如果第一級(jí)存在第二級(jí)菜單,并且沒有第三級(jí)菜單
    if (self.arrow == YES) {
        
        if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:secondIndex:andsecondIndexName:)]) {
            
            [_delegate menuCellDidSelected:_lastSelectedButtonIndex secondIndex:self.lastSelectedCellIndex andsecondIndexName:_dataSourceSecond[self.lastSelectedCellIndex]];
        }
        
    }else if (self.hasThirdTableView == YES){
        
        if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:secondIndex:andsecondIndexName:)]) {
            
            [_delegate menuCellDidSelected:_lastSelectedButtonIndex secondIndex:self.lastSelectedCellIndex andsecondIndexName:_dataSourceSecond[self.lastSelectedCellIndex]];
        }
        
    }else{
        
        [btn setTitle:_dataSourceSecond[self.lastSelectedCellIndex] forState:UIControlStateNormal];
        btn.selected = YES;
        [self remover];
        NSLog(@"Selectsecond RowName is : %@",_dataSourceSecond[self.lastSelectedCellIndex]);
        
        if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:andCategoryName:)]) {
            
            [_delegate menuCellDidSelected:_lastSelectedButtonIndex andCategoryName:_dataSourceSecond[self.lastSelectedCellIndex]];
        }
    }

}else if (tableView == self.tableThird){

    self.hasThirdTableView = NO;
    if (self.arrow == NO) {
        
        [btn setTitle:_dataSourceThird[self.lastSelectedCellIndex] forState:UIControlStateNormal];
        btn.selected = YES;
        [self remover];
        NSLog(@"Selectthird RowName is : %@",_dataSourceThird[self.lastSelectedCellIndex]);
        
        if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:andCategoryName:)]) {
            
            [_delegate menuCellDidSelected:_lastSelectedButtonIndex andCategoryName:_dataSourceThird[self.lastSelectedCellIndex]];
        }
        
    }else{
        
        if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:thirdIndex:andthirdIndexName:)]) {
            
            [_delegate menuCellDidSelected:_lastSelectedButtonIndex thirdIndex:self.self.lastSelectedCellIndex andthirdIndexName:_dataSourceThird[self.lastSelectedCellIndex]];
        }
    }
}

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

if (section == 0) {
    
    return 1;
}
return 0;
}


//分割線頂頭顯示

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

//Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
    
    [cell setSeparatorInset:UIEdgeInsetsZero];
}
//Prevent the cell from inheriting the Table View's margin settings(防止單元格繼承表視圖的邊緣設(shè)置)
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
    
    [cell setPreservesSuperviewLayoutMargins:NO];
}
//Explictly set your cell's layout margins(設(shè)置你的cell的布局空間)
if ([cell respondsToSelector:@selector(setLayoutMargins:)]){
    
    [cell setLayoutMargins:UIEdgeInsetsZero];
    
}
}
在removeDropdownMenuNotifacation接收到要求隱藏下拉菜單時(shí)響應(yīng):
-(void)removeDropdownMenu:(NSNotification *)noti{

NSString * value = noti.object;

if ([value isEqualToString:@"1"]) {
    
    [self remover];
}
}

#pragma mark -- 點(diǎn)擊屏幕隱藏菜單
- (void)remover{
  
[self DefalutTriangleImage];

[self hideCarverView];
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

if ([touch.view isKindOfClass:[UIButton class]] || [NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCell"] || [NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
    
    return NO;
    
}else{
    
    return YES;
}

}

移除通知:
 -(void)dealloc{

 [[NSNotificationCenter defaultCenter] removeObserver:self name:removeDropdownMenuNotifacation object:nil];
}
刷新tableview:
-(void)reloadTableView:(UITableView *)tableView{

[tableView reloadData];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末眶拉,一起剝皮案震驚了整個(gè)濱河市千埃,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌忆植,老刑警劉巖放可,帶你破解...
    沈念sama閱讀 211,817評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異朝刊,居然都是意外死亡耀里,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,329評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門拾氓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來冯挎,“玉大人,你說我怎么就攤上這事咙鞍》抗伲” “怎么了?”我有些...
    開封第一講書人閱讀 157,354評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵续滋,是天一觀的道長翰守。 經(jīng)常有香客問我,道長疲酌,這世上最難降的妖魔是什么蜡峰? 我笑而不...
    開封第一講書人閱讀 56,498評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮朗恳,結(jié)果婚禮上湿颅,老公的妹妹穿的比我還像新娘。我一直安慰自己粥诫,他們只是感情好油航,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,600評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著怀浆,像睡著了一般劝堪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上揉稚,一...
    開封第一講書人閱讀 49,829評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音熬粗,去河邊找鬼搀玖。 笑死,一個(gè)胖子當(dāng)著我的面吹牛驻呐,可吹牛的內(nèi)容都是我干的灌诅。 我是一名探鬼主播芳来,決...
    沈念sama閱讀 38,979評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼猜拾!你這毒婦竟也來了即舌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,722評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤挎袜,失蹤者是張志新(化名)和其女友劉穎顽聂,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盯仪,經(jīng)...
    沈念sama閱讀 44,189評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡紊搪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,519評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了全景。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片耀石。...
    茶點(diǎn)故事閱讀 38,654評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖爸黄,靈堂內(nèi)的尸體忽然破棺而出滞伟,到底是詐尸還是另有隱情,我是刑警寧澤炕贵,帶...
    沈念sama閱讀 34,329評(píng)論 4 330
  • 正文 年R本政府宣布梆奈,位于F島的核電站,受9級(jí)特大地震影響鲁驶,放射性物質(zhì)發(fā)生泄漏鉴裹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,940評(píng)論 3 313
  • 文/蒙蒙 一钥弯、第九天 我趴在偏房一處隱蔽的房頂上張望径荔。 院中可真熱鬧,春花似錦脆霎、人聲如沸总处。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,762評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鹦马。三九已至,卻和暖如春忆肾,著一層夾襖步出監(jiān)牢的瞬間荸频,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,993評(píng)論 1 266
  • 我被黑心中介騙來泰國打工客冈, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旭从,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,382評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像和悦,于是被迫代替她去往敵國和親退疫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,543評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容