1. 程式人生 > >iOS-簡單的二級聯動選單

iOS-簡單的二級聯動選單

二級聯動,可以左側是一個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

// 版權屬於原作者

// 釋出程式碼於最專業的原始碼分享網站: 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

// 版權屬於原作者

// 釋出程式碼於最專業的原始碼分享網站: 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

// 版權屬於原作者

// 釋出程式碼於最專業的原始碼分享網站: 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{

returnleftDataSource.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 = [tableView dequeueReusableCellWithIdentifier:@"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