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;
}
//
// 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
- iOS-简单的二级联动菜单
- 简单的二级联动菜单
- 简单的二级联动菜单
- 非常简单的二级联动菜单 [收藏]
- 简单二级联动菜单实现
- 二级联动菜单,简单实现
- 二级联动菜单,简单实现
- 非常简单的二级联动js菜单 [收藏]
- 一个简单的Ajax二级联动下拉菜单
- [js]实现简单的省市二级联动下拉选择菜单
- 用JavaScript写一个简单的二级联动菜单
- 二级联动菜单--常见的城市二级联动
- 一个简单的二级联动
- 简单的ThinkPHP 二级联动
- 简单的js二级联动
- 简单的二级菜单
- 简单的联动菜单
- javascript 联动菜单V1.0(简单二级菜单)
- 有关Java中try catch无法在while循环中使用的情况
- [C练习]my_atoi函数实现
- ASP操作Excel
- 如何获取textarea标签里面的文本 在线等回复
- fragment实现底部标签 如何使底部标签一直存在
- iOS-简单的二级联动菜单
- android 开发中的一个问题,不知道怎么写标题,大神们进来看看吧!
- ToolBar使用详解+ToolBar按钮颜色修正方法
- 从 Activity B 的 Fragment B 中点击 “按钮” 跳转到 MainActivity 的 Fragment A
- ios 中NSData 拼接成功,但写入本地种只有前面的data
- android通知
- motan学习笔记 五 opentracing学习入门
- 【笔记】OpenSSL for Windows 编译
- android端和pc端使用usb进行socket通信,其中android是服务器端,pc是客户端。如何实现安卓端输入的数据通过按钮发送到pc端?