iOS-简单的二级联动菜单

来源:互联网 发布:财务报表软件免费版 编辑:程序博客网 时间:2024/05/22 01:46

二级联动,可以左侧是一个tabview右侧也是一个tabview,也可以左侧一排按钮,右侧tabview,点击左侧,刷新右侧数据,这一步很简单,我当时不理解的是右侧数据滑动,左侧怎么刷新呢,这需要考虑到tabview的加载方式是动态的,也就是说,当新的数据在右侧tabview加载的时候,比如一个新的section加载的时候,一定会走这个方法:

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (tableView.tag ==21) {

        if (isScrollSetSelect ==YES) {

            [leftScrollViewsetSelectButtonWithIndexPathSection:section];

        }

        return [selfviewForHeaderView:section];

    }else{

        returnnil;

    }

}

/实际需要会修改

-(UIView*)viewForHeaderView:(NSInteger)parama{

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, kScreenWidth,32)];

    label.backgroundColor = [UIColorgrayColor];

    if (leftDataSource.count !=0) {

        label.text =leftDataSource[parama];

        //        [NSString stringWithFormat:@"%ld",(long)parama];

    }

    return label;

}

这个就是实现二级联动的重要核地方,当右侧滑动,需要新的数组加载的时候,当创建新的section头部视图时候,在这个时候可以刷新左侧的视图。





//

//  LeftSelectScroll.h

//  YiLeHelp

//

//  Created by ChenYi on 15/11/14.

//  Copyright © 2015 JC. All rights reserved.

//

//尺寸定义

#define kScreenWidth [UIScreen mainScreen].bounds.size.width//屏幕的宽度

#define kScreenHeight [[UIScreen mainScreen] bounds].size.height//屏幕的高度

#define kNav_H kScreenHeight > 668 ?86 : 64//屏幕的高度

#define kTabbar_H kScreenHeight > 668 ?59 : 49//屏幕的高度



#import <UIKit/UIKit.h>

/*

@protocol LeftSelectScrollDataSource <NSObject>


- (NSInteger)numberOfRowsInSection;


- (UIButton*)viewForRowAtIndexPath:(NSInteger *)indexPath;


@end

*/

@protocol LeftSelectScrollDelegate <NSObject>


-(void)clickLeftSelectScrollButton:(NSInteger)indexPath;


@end


@interface LeftSelectScroll : UIScrollView


@property(nonatomic,strong)NSArray *leftSelectArray;


@property (nonatomic,strong)id<LeftSelectScrollDelegate>leftSelectDelegate;


-(void)setLeftSelectArray:(NSArray *)leftSelectArray;



-(void)setSelectButtonWithIndexPathSection:(NSInteger)indexPathSection;


// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)





@end

// 版权属于原作者

// http://code4app.com (cn)http://code4app.net (en)

// 发布代码于最专业的源码分享网站: Code4App.com

//

//  LeftSelectScroll.m

//  YiLeHelp

//

//  Created by ChenYi on 15/11/14.

//  Copyright © 2015 JC. All rights reserved.

//


#import "LeftSelectScroll.h"


@implementation LeftSelectScroll

{

    UIButton *tempSelectButton;

}



-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [superinitWithFrame:frame]) {

        self.userInteractionEnabled =YES;

        tempSelectButton = [[UIButtonalloc]init];


    }

    returnself;

}


-(void)setLeftSelectArray:(NSArray *)leftSelectArray{

    _leftSelectArray = leftSelectArray;

//    NSArray *array = @[@"套餐",@"饮料",@"点心",@"小菜"];

//    _leftSelectArray = array;

    

    for (int i =0; i<_leftSelectArray.count; i++) {

        UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(0,53*i, kScreenWidth*0.25,53)];

        [button setTitle:_leftSelectArray[i]forState:UIControlStateNormal];

        [button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        [button setTitleColor:[UIColorwhiteColor] forState:UIControlStateSelected];

        

        [button setBackgroundColor:[UIColorwhiteColor]];

        

        UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0, button.frame.size.height -0.5, button.frame.size.width,0.5)];

//        label.backgroundColor = MYCOLOR_LineColor;

        label.backgroundColor = [UIColorgrayColor];

        

        [button addSubview:label];

        

        [selfaddSubview:button];

        

        [button addTarget:selfaction:@selector(clickLeftSelectButton:)forControlEvents:UIControlEventTouchUpInside];

        button.tag = i+11;

        if (i ==0) {

            [button setSelected:YES];

            [button setBackgroundColor:[UIColororangeColor]];

            tempSelectButton = button;

        }

    }

}


-(void)clickLeftSelectButton:(UIButton*)button{

    

    [tempSelectButtonsetSelected:NO];

    [tempSelectButtonsetBackgroundColor:[UIColorwhiteColor]];

    

    [button setBackgroundColor:[UIColorblueColor]];

    [button setSelected:YES];

    

    tempSelectButton = button;

    

    NSInteger tag = button.tag -11;

    if (self.leftSelectDelegate && [self.leftSelectDelegaterespondsToSelector:@selector(clickLeftSelectScrollButton:)]) {

        [self.leftSelectDelegateclickLeftSelectScrollButton:tag];

    }

}


-(void)setSelectButtonWithIndexPathSection:(NSInteger)indexPathSection{


    for (int i =0; i< _leftSelectArray.count; i++) {

        NSInteger tag = i +11 ;

        

        UIButton *btn = (UIButton*)[selfviewWithTag:tag];

        if (btn.tag == indexPathSection +11) {

            tempSelectButton = btn;

            [btn setSelected:YES];

            btn.backgroundColor = [UIColorblueColor];

        }else{

            [btn setSelected:NO];

            btn.backgroundColor = [UIColorwhiteColor];

        }

    }

    

}


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end

// 版权属于原作者

// http://code4app.com (cn)http://code4app.net (en)

// 发布代码于最专业的源码分享网站: Code4App.com

//

//  DetailsViewController.h

//  yileDemo

//

//  Created by ChenYi on 15/12/16.

//  Copyright © 2015 ChenYi. All rights reserved.

//


//尺寸定义

#define kScreenWidth [UIScreen mainScreen].bounds.size.width//屏幕的宽度

#define kScreenHeight [[UIScreen mainScreen] bounds].size.height//屏幕的高度

#define kNav_H kScreenHeight > 668 ?86 : 64//屏幕的高度

#define kTabbar_H kScreenHeight > 668 ?59 : 49//屏幕的高度

//RGBA设置颜色

#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]



#import <UIKit/UIKit.h>


@interface DetailsViewController :UIViewController


@end

// 版权属于原作者

// http://code4app.com (cn)http://code4app.net (en)

// 发布代码于最专业的源码分享网站: Code4App.com

//

//  DetailsViewController.m

//  yileDemo

//

//  Created by ChenYi on 15/12/16.

//  Copyright © 2015 ChenYi. All rights reserved.

//


#import "DetailsViewController.h"

#import "LeftSelectScroll.h"


@interface DetailsViewController ()<LeftSelectScrollDelegate,UITableViewDataSource,UITableViewDelegate>

{

    LeftSelectScroll *leftScrollView;

    

    NSMutableArray *leftDataSource;


    //当点击的时候不去调用滑动调节

    BOOL isScrollSetSelect;


    UITableView *tableViewList;


}

@end


@implementation DetailsViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    [selfinitObjects];

    

    [selfcreatLeftScrollView];

    

    [selfcreateTableView];


    // Do any additional setup after loading the view.

}


-(void)initObjects{

    leftDataSource = [[NSMutableArrayalloc]initWithObjects:@"套餐1",@"套餐2",@"套餐3",@"套餐4",nil];


}

-(void)createTableView{

    //设置右侧菜单frame  -(kTabbar_H)底部那部分高度

    tableViewList = [[UITableViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(leftScrollView.frame),kNav_H, kScreenWidth*0.75,kScreenHeight - (kNav_H))];

    tableViewList.delegate =self;

    tableViewList.dataSource =self;

    tableViewList.tag =21;//标识tableView

    [self.viewaddSubview:tableViewList];

    tableViewList.separatorStyle =UITableViewCellSeparatorStyleNone;

    

    tableViewList.scrollEnabled =YES;

}

-(void)creatLeftScrollView{

    

    leftScrollView = [[LeftSelectScrollalloc]initWithFrame:CGRectMake(0,0, kScreenWidth*0.25,kScreenHeight-(kNav_H)-(kTabbar_H))];

    

    leftScrollView.backgroundColor = [UIColorwhiteColor];

    

    [leftScrollViewsetLeftSelectArray:leftDataSource];

    

    leftScrollView.leftSelectDelegate =self;

    

    leftScrollView.delegate =self;

    

    [self.viewaddSubview:leftScrollView];

}


#pragma mark 点击左侧切换右侧的代理方法

-(void)clickLeftSelectScrollButton:(NSInteger)indexPath{


    isScrollSetSelect =NO;

    

    [tableViewListscrollToRowAtIndexPath:[NSIndexPathindexPathForRow:0inSection:indexPath] atScrollPosition:UITableViewScrollPositionTopanimated:YES];

}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (tableView.tag ==21) {

        if (isScrollSetSelect ==YES) {

            [leftScrollViewsetSelectButtonWithIndexPathSection:section];

        }

        return [selfviewForHeaderView:section];

    }else{

        returnnil;

    }

}



//实际需要会修改

-(UIView*)viewForHeaderView:(NSInteger)parama{

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, kScreenWidth,32)];

    label.backgroundColor = [UIColorgrayColor];

    if (leftDataSource.count !=0) {

        label.text =leftDataSource[parama];

        //        [NSString stringWithFormat:@"%ld",(long)parama];

    }

    return label;

}

#pragma mark UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    

        return leftDataSource.count ;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    return5;

    

}

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

    return25;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return64;

}

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

    

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"tableViewCellIdentF"];

    if (cell ==nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"tableViewCellIdentF"];

    }

    cell.backgroundColor =RGBA(150*(indexPath.section +1 ), 50*(indexPath.section +1 ) , 25*(indexPath.section +1 ),1);

    cell.textLabel.text = [NSStringstringWithFormat:@"菜品%ld",indexPath.row + 1];

    return cell;

}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    isScrollSetSelect =YES ;

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





1 1