左右表格的联动

//MARK: - 一个方法就能搞定 右边滑动时跟左边的联动

- (void)scrollViewDidScroll:(UIScrollView*)scrollView {


    // 如果是 左侧的 tableView 直接return

    if(scrollView ==self.leftTableView)return;


    // 取出显示在 视图 且最靠上 的 cell 的 indexPath

    NSIndexPath*topHeaderViewIndexpath = [[self.rightTableViewindexPathsForVisibleRows]firstObject];


    // 左侧 talbelView 移动的 indexPath

    NSIndexPath*moveToIndexpath = [NSIndexPathindexPathForRow:topHeaderViewIndexpath.sectioninSection:0];


    // 移动 左侧 tableView 到 指定 indexPath 居中显示

    [self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];


}

//MARK: - 点击 cell 的代理方法

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {


    // 选中 左侧 的 tableView

    if(tableView ==self.leftTableView) {


        NSIndexPath*moveToIndexPath = [NSIndexPathindexPathForRow:0inSection:indexPath.row];


        // 将右侧 tableView 移动到指定位置

        [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];


        // 取消选中效果

        [self.rightTableViewdeselectRowAtIndexPath:moveToIndexPathanimated:YES];

    }

}

#pragma mark - 懒加载 tableView -

// MARK: - 左边的 tableView

- (UITableView*)leftTableView {


    if (!_leftTableView) {


        UITableView*tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,leftTableWidth,ScreenHeight)];


        [self.viewaddSubview:tableView];


        _leftTableView= tableView;


        tableView.dataSource=self;

        tableView.delegate=self;


        [tableViewregisterClass:[UITableViewCell class] forCellReuseIdentifier:leftCellIdentifier];

        tableView.backgroundColor= [UIColorredColor];

        tableView.tableFooterView= [[UIViewalloc]init];


    }

    return _leftTableView;

}

// MARK: - 右边的 tableView

- (UITableView*)rightTableView {


    if (!_rightTableView) {


        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(leftTableWidth, 0, rightTableWidth, ScreenHeight)];


        [self.viewaddSubview:tableView];


        _rightTableView= tableView;


        tableView.dataSource=self;

        tableView.delegate=self;


        [tableViewregisterClass:[UITableViewCell class] forCellReuseIdentifier:rightCellIdentifier];

        tableView.backgroundColor= [UIColorcyanColor];

        tableView.tableFooterView= [[UIViewalloc]init];


    }

    return _rightTableView;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 13,655评论 3 3
  • 导入 #define leftTableWidth [UIScreen mainScreen].bounds.si...
    leaderleader阅读 1,163评论 0 0
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,951评论 3 38
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改变导航栏背景颜...
    SadMine阅读 5,644评论 1 4
  • 1一个老男人经常到一家小饭店里吃饭,他最喜欢饭店老板亲手做的黄油素面。 这个男人不简单,他是一个著名的美食评论家。...
    云程大叔阅读 2,929评论 0 0