iOS-简单的二级联动菜单

这篇博客介绍了如何在iOS应用中实现二级联动菜单。通过使用UITableView,当右侧菜单滑动时,左侧菜单能根据选中section自动刷新。具体实现涉及UITableView的代理方法,包括`viewForHeaderInSection`和`tableView:didSelectRowAtIndexPath:`,以及自定义的LeftSelectScroll组件来处理左侧选中按钮的状态更新。
摘要由CSDN通过智能技术生成

二级联动,可以左侧是一个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(0, 0, kScreenWidth, 32)];

    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)

// 发布代码于最专业的源码分享网站

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值