tableview联动效果

- (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

{

    // 当前的tableViewRightTableViewRightTableView滚动的方向向上,RightTableView是用户拖拽而产生滚动的((主要判断RightTableView用户拖拽而滚动的,还是点击LeftTableView而滚动的)

    if ((self.righttableView == tableView) && !_isScrollDown &&self.righttableView.dragging)

    {

        [selfselectRowAtIndexPath:section];

    }

}


// TableView分区标题展示结束

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section

{

    // 当前的tableViewRightTableViewRightTableView滚动的方向向下,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



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值