二级联动

本文介绍了一种在iOS应用中实现二级联动效果的方法,通过左侧按钮或TabView与右侧TabView的数据联动,确保数据同步刷新。重点讲解了如何在右侧TabView滑动时更新左侧视图,涉及UITableView数据源方法的重写。

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

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

    if (tableView.tag == 21) {

        if (isScrollSetSelect == YES) {

            [leftScrollView setSelectButtonWithIndexPathSection:section];

        }

        return [self viewForHeaderView:section];

    }else{

        return nil;

    }

}

/实际需要会修改

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

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(00kScreenWidth32)];

    label.backgroundColor = [UIColor grayColor];

    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 = [super initWithFrame:frame]) {

        self.userInteractionEnabled = YES;

        tempSelectButton = [[UIButton alloc]init];


    }

    return self;

}


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

    _leftSelectArray = leftSelectArray;

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

//    _leftSelectArray = array;

    

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

        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(053*i, kScreenWidth*0.2553)];

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

        [button setTitleColor:[UIColor blackColorforState:UIControlStateNormal];

        [button setTitleColor:[UIColor whiteColorforState:UIControlStateSelected];

        

        [button setBackgroundColor:[UIColor whiteColor]];

        

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

//        label.backgroundColor = MYCOLOR_LineColor;

        label.backgroundColor = [UIColor grayColor];

        

        [button addSubview:label];

        

        [self addSubview:button];

        

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

        button.tag = i+11;

        if (i == 0) {

            [button setSelected:YES];

            [button setBackgroundColor:[UIColor orangeColor]];

            tempSelectButton = button;

        }

    }

}


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

    

    [tempSelectButton setSelected:NO];

    [tempSelectButton setBackgroundColor:[UIColor whiteColor]];

    

    [button setBackgroundColor:[UIColor blueColor]];

    [button setSelected:YES];

    

    tempSelectButton = button;

    

    NSInteger tag = button.tag - 11;

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

        [self.leftSelectDelegate clickLeftSelectScrollButton:tag];

    }

}


-(void)setSelectButtonWithIndexPathSection:(NSInteger)indexPathSection{


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

        NSInteger tag = i + 11 ;

        

        UIButton *btn = (UIButton*)[self viewWithTag:tag];

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

            tempSelectButton = btn;

            [btn setSelected:YES];

            btn.backgroundColor = [UIColor blueColor];

        }else{

            [btn setSelected:NO];

            btn.backgroundColor = [UIColor whiteColor];

        }

    }

    

}


/*

// 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 {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    [self initObjects];

    

    [self creatLeftScrollView];

    

    [self createTableView];


    // Do any additional setup after loading the view.

}


-(void)initObjects{

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


}

-(void)createTableView{

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

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

    tableViewList.delegate = self;

    tableViewList.dataSource = self;

    tableViewList.tag = 21;//标识tableView

    [self.view addSubview:tableViewList];

    tableViewList.separatorStyle = UITableViewCellSeparatorStyleNone;

    

    tableViewList.scrollEnabled = YES;

}

-(void)creatLeftScrollView{

    

    leftScrollView = [[LeftSelectScroll alloc]initWithFrame:CGRectMake(00kScreenWidth*0.25kScreenHeight-(kNav_H)-(kTabbar_H))];

    

    leftScrollView.backgroundColor = [UIColor whiteColor];

    

    [leftScrollView setLeftSelectArray:leftDataSource];

    

    leftScrollView.leftSelectDelegate = self;

    

    leftScrollView.delegate = self;

    

    [self.view addSubview:leftScrollView];

}


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

-(void)clickLeftSelectScrollButton:(NSInteger)indexPath{


    isScrollSetSelect = NO;

    

    [tableViewList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}


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

    if (tableView.tag == 21) {

        if (isScrollSetSelect == YES) {

            [leftScrollView setSelectButtonWithIndexPathSection:section];

        }

        return [self viewForHeaderView:section];

    }else{

        return nil;

    }

}



//实际需要会修改

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

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(00kScreenWidth32)];

    label.backgroundColor = [UIColor grayColor];

    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{

    

    return 5;

    

}

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

    return 25;

}


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

    return 64;

}

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

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCellIdentF"];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"tableViewCellIdentF"];

    }

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

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

    return cell;

}

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

    isScrollSetSelect = YES ;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // 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


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值