-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableViewController.m
280 lines (220 loc) · 9.88 KB
/
TableViewController.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// TableViewController.m
// 两表联动
//
// Created by lei on 2016/10/12.
// Copyright © 2016年 lei. All rights reserved.
//
//屏幕尺寸
#define kDeviceWidth [[UIScreen mainScreen]bounds].size.width
#define kDeviceHight [[UIScreen mainScreen]bounds].size.height
#import "TableViewController.h"
#import "CategoryModel.h"
#import "LeftTableViewCell.h"
#import "RightTableViewCell.h"
#import "TableViewHeaderView.h"
static NSString * const LeftCellID = @"LeftCellID";
static NSString * const RightCellID = @"RightCellID";
@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource,CAAnimationDelegate>{
NSInteger _selectIndex;
BOOL _isScrollDown;
}
@property (strong, nonatomic) NSMutableArray<CALayer *> *redLayers;
@property (nonatomic, strong) NSMutableArray *LeftDataArrM;
@property (nonatomic, strong) NSMutableArray *RightDataArrM;
@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) UITableView *rightTableView;
@end
@implementation TableViewController
- (NSMutableArray<CALayer *> *)redLayers {
if (!_redLayers) {
_redLayers = [NSMutableArray array];
}
return _redLayers;
}
-(NSMutableArray *)LeftDataArrM{
if (!_LeftDataArrM) {
_LeftDataArrM = [NSMutableArray array];
}
return _LeftDataArrM ;
}
-(NSMutableArray *)RightDataArrM{
if (!_RightDataArrM) {
_RightDataArrM = [NSMutableArray array];
}
return _RightDataArrM;
}
- (UITableView *)leftTableView {
if (!_leftTableView) {
_leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 80, kDeviceHight)];
_leftTableView.delegate = self;
_leftTableView.dataSource = self;
_leftTableView.rowHeight = 55;
_leftTableView.tableFooterView = [UIView new];
_leftTableView.showsVerticalScrollIndicator = NO;
_leftTableView.separatorColor = [UIColor clearColor];
[_leftTableView registerClass:[LeftTableViewCell class] forCellReuseIdentifier:LeftCellID];
}
return _leftTableView;
}
- (UITableView *)rightTableView {
if (!_rightTableView) {
_rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(80, 64, kDeviceWidth - 80, kDeviceHight)];
_rightTableView.delegate = self;
_rightTableView.dataSource = self;
_rightTableView.rowHeight = 80;
_rightTableView.tableFooterView = [UIView new];
_rightTableView.showsVerticalScrollIndicator = NO;
[_rightTableView registerClass:[RightTableViewCell class] forCellReuseIdentifier:RightCellID];
}
return _rightTableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
_selectIndex = 0;
_isScrollDown = YES;
//加载数据
[self loadData];
//创建表格
[self createTable];
}
- (void)initCHLayerFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint {
//UIView主要是对显示内容的管理而 CALayer 主要侧重显示内容的绘制。
CALayer *chLayer = [[CALayer alloc] init];
[self.redLayers addObject:chLayer];
//起始位置.大小
chLayer.frame = CGRectMake(startPoint.x, startPoint.y, 15, 15);
//设置一个圆
chLayer.cornerRadius = CGRectGetWidth(chLayer.frame)/2.f;
//颜色为红色
chLayer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:chLayer];
//关键帧动画
CAKeyframeAnimation *CHAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
//创建路径(是基于路径)
CGMutablePathRef path = CGPathCreateMutable();
//起点开始绘制
CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
//结束
CGPathAddQuadCurveToPoint(path, NULL, endPoint.x, startPoint.y, endPoint.x, endPoint.y);
CHAnimation.path = path;
//设置是否动画完成后,动画效果从设置的layer上移除。默认为YES
CHAnimation.removedOnCompletion = NO;
//设置类型,默认是删除
CHAnimation.fillMode = kCAFillModeBoth;
//时间
CHAnimation.duration = 0.5;
//要设置代理
CHAnimation.delegate = self;
//最后添加到CALayer上
[chLayer addAnimation:CHAnimation forKey:nil];
}
-(void)loadData{
NSString *path = [[NSBundle mainBundle]pathForResource:@"meituan.json" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@",dict);
NSArray * arr = dict[@"data"][@"food_spu_tags"];
for (NSDictionary *dicts in arr) {
CategoryModel *model = [CategoryModel objectWithDictionary:dicts];
[self.LeftDataArrM addObject:model];
NSMutableArray * d = [NSMutableArray array];
for (NSDictionary *ds in model.spus) {
[d addObject:ds];
}
[self.RightDataArrM addObject:d];
}
}
-(void)createTable{
[self.view addSubview:self.leftTableView];
[self.view addSubview:self.rightTableView];
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return (self.leftTableView==tableView) ? 1 : self.LeftDataArrM.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return (self.leftTableView == tableView) ? self.LeftDataArrM.count : [self.RightDataArrM[section]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.leftTableView == tableView) {
LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LeftCellID forIndexPath:indexPath];
CategoryModel *model = self.LeftDataArrM[indexPath.row];
cell.name.text = model.name;
return cell;
}else {
RightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RightCellID forIndexPath:indexPath];
FoodModel *model = self.RightDataArrM[indexPath.section][indexPath.row];
cell.model = model;
[cell btnBlockReturn:^(NSInteger x) {
x = indexPath.row;
NSLog(@"%ld",x);
FoodModel *model = self.RightDataArrM[indexPath.section][x];
NSLog(@"%@",model.foodId);
}];
[cell setPlusTapHandle:^(CGPoint position) {
CGPoint startPoint = [self.view convertPoint:position fromView:self.rightTableView];
CGPoint endPoint = [self.view convertPoint:CGPointMake(0, kDeviceHight) fromView:self.view];
[self initCHLayerFromPoint:startPoint toPoint:endPoint];
//model add to array
}];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (self.rightTableView == tableView) ? 20 : 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
TableViewHeaderView * view = [[TableViewHeaderView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, 20)];
CategoryModel * model = self.LeftDataArrM[section];
view.name.text = model.name;
return (self.rightTableView == tableView) ? view: nil;
}
// TableView分区标题即将展示
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
// 当前的tableView是RightTableView,RightTableView滚动的方向向上,RightTableView是用户拖拽而产生滚动的((主要判断RightTableView用户拖拽而滚动的,还是点击LeftTableView而滚动的)
if ((_rightTableView == tableView) && !_isScrollDown && _rightTableView.dragging) {
[self selectRowAtIndexPath:section];
}
}
// TableView分区标题展示结束
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
// 当前的tableView是RightTableView,RightTableView滚动的方向向下,RightTableView是用户拖拽而产生滚动的((主要判断RightTableView用户拖拽而滚动的,还是点击LeftTableView而滚动的)
if ((_rightTableView == tableView) && _isScrollDown && _rightTableView.dragging)
{
[self selectRowAtIndexPath:section + 1];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_leftTableView != tableView) return;
_selectIndex = indexPath.row;
[_rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:_selectIndex] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
// 当拖动右边TableView的时候,处理左边TableView
- (void)selectRowAtIndexPath:(NSInteger)index {
[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
// 标记一下RightTableView的滚动方向,是向上还是向下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
static CGFloat lastOffsetY = 0;
UITableView *tableView = (UITableView *)scrollView;
if (_rightTableView == tableView) {
_isScrollDown = lastOffsetY < scrollView.contentOffset.y;
lastOffsetY = scrollView.contentOffset.y;
}
}
- (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