tableview二级联动
来源:互联网 发布:南风知我意结局是什么 编辑:程序博客网 时间:2024/06/04 19:08
#import "ViewController.h"
#import "leftViewController.h"
@interfaceViewController()
@end
@implementationViewController
-(void)viewDidLoad{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor=[UIColorwhiteColor];
UIButton*bution=[[UIButtonalloc]initWithFrame:CGRectMake(0,0,100,50)];
bution.center=self.view.center;
bution.backgroundColor=[UIColorredColor];
[bution addTarget:self action:@selector(butionCleck:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bution];
}
-(void)butionCleck:(UIButton*)sender{
leftViewController*leftVC=[leftViewControllernew];
[self.navigationController pushViewController:leftVC animated:YES];
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
leftViewController.h
1
2
3
#import
@interface leftViewController : UIViewController
@end
leftViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#import "leftViewController.h"
#import "rightViewController.h"
@interfaceleftViewController()
@property(nonatomic,strong)UITableView*leftTableView;
@property(nonatomic,strong)NSArray*leftDataArr;
@property(nonatomic,strong)rightViewController*rightVC;
@end
@implementationleftViewController
-(void)viewDidLoad{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColorwhiteColor];
[self.view addSubview:self.leftTableView];
[selfcreatRightVC];
}
-(void)creatRightVC{
self.rightVC=[rightViewControllernew];
self.rightVC.delegate=self;
[self addChildViewController:_rightVC];
[self.view addSubview:_rightVC.view];
}
-(UITableView*)leftTableView{
if(!_leftTableView){
_leftTableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,120,self.view.frame.size.height) style:UITableViewStylePlain];
_leftTableView.showsVerticalScrollIndicator=NO;
_leftTableView.delegate=self;
_leftTableView.dataSource=self;
[_leftTableView registerClass:[UITableViewCellclass] forCellReuseIdentifier:@"cell11"];
}
return_leftTableView;
}
-(NSArray*)leftDataArr{
if(!_leftDataArr){
_leftDataArr=@[@"第一类",
@"第二类",
@"第三类",
@"第四类",
@"第五类",
@"第六类",
@"第七类",
@"第八类",
@"第九类",
@"第十类",
@"第十一类",
@"第十二类",
@"第十三类",
@"第十四类",
@"第十五类",
@"第十六类",
@"第十七类",
@"第十八类",
@"第十九类",
@"第二十类",
@"第二十一类"
];
}
return_leftDataArr;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnself.leftDataArr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell11" forIndexPath:indexPath];
cell.textLabel.text=self.leftDataArr[indexPath.row];
returncell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"-----%ld",indexPath.row);
if(self.rightVC){
[self.rightVC scrollToSelectIndexPath:indexPath];
}
}
/*
代理右边选择时执行代理.
*/
-(void)rightViewControllerDelegate:(rightViewController*)vc withIndexPatch:(NSIndexPath*)ptch{
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:ptch.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
-(void)rightViewControllerDelegateHeaderViewAppear:(rightViewController*)vc withIndexPatch:(NSInteger)section{
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
-(void)rightViewControllerDelegateHeaderViewdisappear:(rightViewController*)vc withIndexPatch:(NSInteger)section{
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
rightViewController.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import
@class rightViewController;
@protocol rightViewControllerDeldgate
-(void)rightViewControllerDelegate:(rightViewController *)vc withIndexPatch:(NSIndexPath *)ptch;
-(void)rightViewControllerDelegateHeaderViewAppear:(rightViewController *)vc withIndexPatch:(NSInteger )section;
-(void)rightViewControllerDelegateHeaderViewdisappear:(rightViewController *)vc withIndexPatch:(NSInteger )section;
@end
@interface rightViewController : UIViewController
@property(nonatomic,weak)id delegate;
-(void)scrollToSelectIndexPath:(NSIndexPath *)path;
@end
rightViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#import "rightViewController.h"
@interfacerightViewController()
@property(nonatomic,strong)UITableView*rightTableView;
@property(nonatomic,strong)NSArray*rightDataArr;
@property(nonatomic,assign)BOOLisScrollUp;
@property(nonatomic,assign)CGFloatlastOffsetY;//滚动即将结束时scrollView的偏移量
@property(nonatomic,assign)BOOLifScroll;//当左边cell点击-> 右边滑动-> 右边滑动头消失头出现就会执行代理->代理方法再让左边滑动 给个boll值设置为NO就是为了 点击左边cell右边头出现或者消失 都不执行代理方法 左边也不会滑动.
@end
@implementationrightViewController
-(UITableView*)rightTableView{
if(!_rightTableView){
_rightTableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
_rightTableView.showsVerticalScrollIndicator=NO;
_rightTableView.delegate=self;
_rightTableView.dataSource=self;
[_rightTableView registerClass:[UITableViewCellclass] forCellReuseIdentifier:@"cell"];
[_rightTableView registerClass:[UITableViewHeaderFooterViewclass] forHeaderFooterViewReuseIdentifier:@"headerView"];
}
return_rightTableView;
}
-(NSArray*)rightDataArr{
if(!_rightDataArr){
_rightDataArr=
@[
@[@"1橘子",@"西瓜",@"哈密瓜",@"苹 果",@"柚子",@"香蕉"],
@[@"2华为手机",@"苹果5",@"苹果5s",@"苹果6",@"苹果6+",@"苹果6s",@"苹果6s+"],
@[@"3联想电脑",@"华硕电脑",@"MAC"],
@[@"4鸡肉",@"鸭肉",@"牛肉",@"猪肉",@"羊肉"],
@[@"5香菇",@"蘑菇",@"大头菜",@"青椒"],
@[@"6花生油",@"大豆油",@"葵花籽油",@"芝 麻香油"],
@[@"7花椒",@"陈皮",@"八角"],
@[@"8橘子",@"西瓜",@"哈密瓜",@"苹果",@"柚子",@"香蕉"],
@[@"9华为手机",@"苹果5",@"苹果5s",@"苹果6",@"苹果6+",@"苹果6s",@"苹果6s+"],
@[@"10联想电脑",@"华硕电脑",@"MAC"],
@[@"11鸡肉",@"鸭肉",@"牛肉",@"猪肉",@"羊肉"],
@[@"12香菇",@"蘑菇",@"大头菜",@"青椒"],
@[@"13花生油",@"大豆油",@"葵花籽油",@"芝麻香油"],
@[@"14花椒",@"陈皮",@"八角"],
@[@"15橘子",@"西瓜",@"哈密瓜",@"苹果",@"柚子",@"香蕉"],
@[@"16华为手机",@"苹果5",@"苹果5s",@"苹果6",@"苹果6+",@"苹果6s",@"苹果6s+"],
@[@"17联想电脑",@"华硕电脑",@"MAC"],
@[@"18鸡肉",@"鸭肉",@"牛肉",@"猪肉",@"羊肉"],
@[@"19香菇",@"蘑菇",@"大头菜",@"青椒"],
@[@"20花生油",@"大豆油",@"葵花籽油",@"芝麻香油"],
@[@"21花椒",@"陈皮",@"八角"]
];
}
return_rightDataArr;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
returnself.rightDataArr.count;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
NSArray*arr=self.rightDataArr[section];
returnarr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSArray*data=self.rightDataArr[indexPath.section];
cell.textLabel.text=data[indexPath.row];
returncell;
}
-(nullableUIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
UITableViewHeaderFooterView*headerView=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
headerView.textLabel.text=[NSString stringWithFormat:@"第%ld区",section+1];
returnheaderView;
}
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
return30;
}
-(void)viewDidLoad{
[superviewDidLoad];
self.view.frame=CGRectMake(120,64,self.view.frame.size.width-120,self.view.frame.size.height-64);
self.view.backgroundColor=[UIColorredColor];
[self.view addSubview:self.rightTableView];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
if(_delegate&&[_delegate respondsToSelector:@selector(rightViewControllerDelegate:withIndexPatch:)]){
[_delegate rightViewControllerDelegate:self withIndexPatch:indexPath];
}
}
//头将出现
-(void)tableView:(UITableView*)tableView willDisplayHeaderView:(UIView*)view forSection:(NSInteger)section{
if(_delegate&&[self.delegate respondsToSelector:@selector(rightViewControllerDelegateHeaderViewAppear:withIndexPatch:)]&&!_isScrollUp&&_ifScroll){
[_delegate rightViewControllerDelegateHeaderViewAppear:self withIndexPatch:section];
}
}
//头讲消失
-(void)tableView:(UITableView*)tableView didEndDisplayingHeaderView:(UIView*)view forSection:(NSInteger)section{
if(_delegate&&[self.delegate respondsToSelector:@selector(rightViewControllerDelegateHeaderViewdisappear:withIndexPatch:)]&&_isScrollUp&&_ifScroll){
//上滑才执行
[_delegate rightViewControllerDelegateHeaderViewdisappear:self withIndexPatch:section+1];
}
}
//主要判断向上滑动还是向下滑动
-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
_isScrollUp=_lastOffsetY
阅读全文
0 0
- 二级tableView联动
- tableview二级联动
- 两个tableView的二级联动
- tableView联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 二级联动
- 268. Missing Number
- Codeforces 813 B The Golden Age
- UE4.17将正式支持ARKit
- 利用Windows服务器发布网站
- 本地项目上传git远程仓库
- tableview二级联动
- 总结三
- 128. Longest Consecutive Sequence
- 内存
- 动态规划略有所得 数字三角形(POJ1163)
- JDK8新特性Lambda入门
- 剑指Offer-28
- 不忘初心,方得始终
- Java 枚举类的基本使用