- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
_selectIndex =0;
_isScrollDown =YES;
NSString *path = [[NSBundlemainBundle]pathForResource:@"meituan"ofType:@"json"];
NSData *data = [NSDatadataWithContentsOfFile:path];
NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingAllowFragmentserror:nil];
NSArray *foods = dict[@"data"][@"food_spu_tags"];
//我使用的是jsonmodel进行解析的(数组的嵌套)
for (NSDictionary *dictin foods) {
CategoryModel *model = [[CategoryModelalloc]initWithDictionary:dict];
[self.catemodelArrayaddObject:model];
NSMutableArray *datas = [NSMutableArrayarray];
for (FoodModel *modelsin model.spus) {
[datas addObject:models];
}
[self.foodmodelArrayaddObject:datas];
}
[self.viewaddSubview:self.lefttableView];
[self.viewaddSubview:self.righttableView];
[self.lefttableViewselectRowAtIndexPath:[NSIndexPathindexPathForRow:0inSection:0]animated:YESscrollPosition:UITableViewScrollPositionTop];
}
-(NSMutableArray *)catemodelArray
{
if (_catemodelArray ==nil) {
_catemodelArray = [NSMutableArrayarray];
}
return_catemodelArray;
}
-(NSMutableArray *)foodmodelArray
{
if (_foodmodelArray ==nil) {
_foodmodelArray = [NSMutableArrayarray];
}
return_foodmodelArray;
}
-(UITableView *)lefttableView
{
if (_lefttableView ==nil) {
_lefttableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,80, SCREEN_HEIGHT)];
_lefttableView.delegate =self;
_lefttableView.dataSource =self;
_lefttableView.showsVerticalScrollIndicator =NO;
_lefttableView.tableFooterView = [UIViewnew];
_lefttableView.separatorColor = [UIColorclearColor];
[_lefttableViewregisterClass:[LeftTableViewCellclass]forCellReuseIdentifier:leftTableViewCellIdentifier];
_lefttableView.rowHeight =55;
}
return_lefttableView;
}
-(UITableView *)righttableView
{
if (_righttableView ==nil) {
_righttableView = [[UITableViewalloc]initWithFrame:CGRectMake(80,64, SCREEN_WIDTH - 80, SCREEN_HEIGHT -64)];
_righttableView.delegate =self;
_righttableView.dataSource =self;
_righttableView.rowHeight =80;
_righttableView.showsVerticalScrollIndicator =NO;
[_righttableViewregisterClass:[RightTableViewCellclass]forCellReuseIdentifier:rightTableViewCellIdentifier];
}
return_righttableView;
}
#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView ==self.lefttableView) {
return1;
}
else
{
returnself.foodmodelArray.count;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView ==self.lefttableView) {
returnself.catemodelArray.count;
}
else {
return [self.foodmodelArray[section]count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView ==self.lefttableView) {
LeftTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:leftTableViewCellIdentifierforIndexPath:indexPath];
CategoryModel *model =self.catemodelArray[indexPath.row];
cell.model = model;
return cell;
}
else
{
RightTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:rightTableViewCellIdentifierforIndexPath:indexPath];
FoodModel *model =self.foodmodelArray[indexPath.section] [indexPath.row];
cell.model = model;
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (tableView ==self.righttableView) {
return20;
}
return0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (self.righttableView == tableView)
{
LeftHeaderView *view = [[LeftHeaderViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,20)];
CategoryModel *model =self.catemodelArray[section];
view.name.text = model.name;
return view;
}
returnnil;
}
// TableView分区标题即将展示
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(nonnullUIView *)view forSection:(NSInteger)section
{
// 当前的tableView是RightTableView,RightTableView滚动的方向向上,RightTableView是用户拖拽而产生滚动的((主要判断RightTableView用户拖拽而滚动的,还是点击LeftTableView而滚动的)
if ((self.righttableView == tableView) && !_isScrollDown &&self.righttableView.dragging)
{
[selfselectRowAtIndexPath:section];
}
}
// TableView分区标题展示结束
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
{
// 当前的tableView是RightTableView,RightTableView滚动的方向向下,RightTableView是用户拖拽而产生滚动的((主要判断RightTableView用户拖拽而滚动的,还是点击LeftTableView而滚动的)
if ((self.righttableView == tableView) && _isScrollDown &&self.righttableView.dragging)
{
[selfselectRowAtIndexPath:section +1];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnullNSIndexPath *)indexPath
{
if (self.lefttableView == tableView)
{
_selectIndex = indexPath.row;
[self.righttableViewscrollToRowAtIndexPath:[NSIndexPathindexPathForRow:0inSection:_selectIndex]atScrollPosition:UITableViewScrollPositionTopanimated:YES];
}
}
// 当拖动右边TableView的时候,处理左边TableView
- (void)selectRowAtIndexPath:(NSInteger)index
{
[self.lefttableViewselectRowAtIndexPath:[NSIndexPathindexPathForRow:indexinSection:0]animated:YESscrollPosition:UITableViewScrollPositionTop];
}
#pragma mark - uiscrollviewDelegate
// 标记一下RightTableView的滚动方向,是向上还是向下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
staticCGFloat lastOffsetY =0;
UITableView *tableView = (UITableView *) scrollView;
if (self.righttableView == tableView)
{
_isScrollDown = lastOffsetY < scrollView.contentOffset.y;
lastOffsetY = scrollView.contentOffset.y;
}
}
//详细代码请见github
https://github.com/homeabc/tableview.git