iOS-简单的二级联动菜单
2017-01-02 08:43
633 查看
二级联动,可以左侧是一个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)
// 发布代码于最专业的源码分享网站: 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(0,
53*i, kScreenWidth*0.25,
53)];
[button setTitle:_leftSelectArray[i]
forState:UIControlStateNormal];
[button setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor
whiteColor] forState:UIControlStateSelected];
[button setBackgroundColor:[UIColor
whiteColor]];
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectMake(0,
button.frame.size.height -
0.5, button.frame.size.width,
0.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.leftSelectDelegate
respondsToSelector:@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_H,
kScreenWidth*0.75,
kScreenHeight - (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(0,
0, kScreenWidth*0.25,
kScreenHeight-(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(0,
0, kScreenWidth,
32)];
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:UITableViewCellStyleDefault
reuseIdentifier:@"tableViewCellIdentF"];
}
cell.backgroundColor =
RGBA(150*(indexPath.section +
1 ), 50*(indexPath.section +
1 ) , 25*(indexPath.section +
1 ),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
相关文章推荐
- JavaScript实际应用:简单二级联动菜单实现
- 一个简单的js的省市二级联动菜单(转)
- javascript 联动菜单V1.0(简单二级菜单)
- JavaScript实际应用:简单二级联动菜单实现
- 利用JS实现一个简单的二级联动菜单
- [js]实现简单的省市二级联动下拉选择菜单
- JavaScript实际应用:简单二级联动菜单实现
- 二级联动菜单,简单实现
- 简单的二级联动菜单
- 用JavaScript写一个简单的二级联动菜单
- 简单二级联动菜单实现
- 二级联动菜单,简单实现
- JavaScript实际应用:简单二级联动菜单实现
- iOS实现简单的二级菜单效果
- 简单的二级联动菜单
- JavaScript实际应用:简单二级联动菜单实现
- 非常简单的二级联动菜单 [收藏]
- 非常简单的二级联动js菜单 [收藏]
- 二级联动菜单,简单实现
- JavaScript实际应用:简单二级联动菜单实现