美文网首页开发iOS学习笔记OSiOS
iOS两个 TableView 联动. 思路简单明了,无bug!

iOS两个 TableView 联动. 思路简单明了,无bug!

作者: haoAku | 来源:发表于2016-08-21 14:47 被阅读7040次

左右两个 TableView 联动

TowTableView.gif

小解析,可以先看看后面的!

Snip20160821_3.png

实现 tableView联动 主要分两种状况

  • 点击 左侧 cell 让右侧 tableView 滚到对应位置
  • 滑动 右侧 tableView 让左侧 tableView 滚到对应位置

1.先实现简单的:点击 左侧 cell 让右侧 tableView 滚到对应位置

//MARK: - 点击 cell 的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 判断是否为 左侧 的 tableView
    if (tableView == self.leftTableView) {
        
        // 计算出 右侧 tableView 将要 滚动的 位置
        NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
        
        // 将 rightTableView 移动到对应的 位置
        [self.rightTableView scrollToRowAtIndexPath:moveToIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
```
**左侧 按钮点击的联动 搞定!**
****
**2.滑动 右侧 tableView 让左侧 tableView 滚到对应位置**
> [self.rightTableView indexPathsForVisibleRows] 返回 所有显示在界面的 cell 的 indexPath

```
//MARK: - 一个方法就能搞定 右边滑动时跟左边的联动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 如果是 左侧的 tableView 直接return
    if (scrollView == self.leftTableView) return;
    
    // 取出显示在 视图 且最靠上 的 cell 的 indexPath
    NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject];
    
    // 左侧 talbelView 移动到的位置 indexPath
    NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];
    
    // 移动 左侧 tableView 到 指定 indexPath 居中显示
    [self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
```
**第二步 右侧 滑动 跟左侧 的联动 搞定! 对的 就是这么简单!!!**

> #警告

**在简书上看到别人通过这两个方法判断!!!  勿用!!!**
会导致 tableView 的联动 不准确
```
#pragma mark - UITableViewDelegate 代理方法 -
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
//
//    headerView 将要显示
//    这两个方法都不准确
}
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
//
//    headerView 已经显示
//    这两个方法都不准确
}
```
#以下是所有代码,很简单的 就不传 github 了
```
//
//  ViewController.m
//  左右双tableView联动
//
//  Created by 阿酷 on 16/8/20.
//  Copyright © 2016年 AkuApp. All rights reserved.
//

#import "ViewController.h"

#define leftTableWidth  [UIScreen mainScreen].bounds.size.width * 0.3
#define rightTableWidth [UIScreen mainScreen].bounds.size.width * 0.7
#define ScreenWidth     [UIScreen mainScreen].bounds.size.width
#define ScreenHeight    [UIScreen mainScreen].bounds.size.height

#define leftCellIdentifier  @"leftCellIdentifier"
#define rightCellIdentifier @"rightCellIdentifier"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *leftTableView;

@property (nonatomic, weak) UITableView *rightTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.leftTableView];
    [self.view addSubview:self.rightTableView];
}


#pragma mark - tableView 数据源代理方法 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.leftTableView) return 40;
    return 8;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (tableView == self.leftTableView) return 1;
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell;
    
    // 左边的 view
    if (tableView == self.leftTableView) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:leftCellIdentifier forIndexPath:indexPath];
        cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];

        // 右边的 view
    } else {
        
        cell = [tableView dequeueReusableCellWithIdentifier:rightCellIdentifier forIndexPath:indexPath];
        cell.textLabel.text = [NSString stringWithFormat:@"第%ld组-第%ld行", indexPath.section, indexPath.row];
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (tableView == self.rightTableView) return [NSString stringWithFormat:@"第 %ld 组", section];
    
    return nil;
}


#pragma mark - UITableViewDelegate 代理方法 -
//- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
//
//    这两个方法都不准确
//}
//
//- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
//
//    这两个方法都不准确
//}

//MARK: - 一个方法就能搞定 右边滑动时跟左边的联动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 如果是 左侧的 tableView 直接return
    if (scrollView == self.leftTableView) return;
    
    // 取出显示在 视图 且最靠上 的 cell 的 indexPath
    NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject];
    
    // 左侧 talbelView 移动的 indexPath
    NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection: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 = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
        
        // 将右侧 tableView 移动到指定位置
        [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
        
        // 取消选中效果
        [self.rightTableView deselectRowAtIndexPath:moveToIndexPath animated:YES];
    }
}

#pragma mark - 懒加载 tableView -
// MARK: - 左边的 tableView
- (UITableView *)leftTableView {
    
    if (!_leftTableView) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, leftTableWidth, ScreenHeight)];
        
        [self.view addSubview:tableView];
        
        _leftTableView = tableView;
        
        tableView.dataSource = self;
        tableView.delegate = self;
        
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:leftCellIdentifier];
        tableView.backgroundColor = [UIColor redColor];
        tableView.tableFooterView = [[UIView alloc] init];
        
    }
    return _leftTableView;
}

// MARK: - 右边的 tableView
- (UITableView *)rightTableView {
    
    if (!_rightTableView) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(leftTableWidth, 0, rightTableWidth, ScreenHeight)];
        
        [self.view addSubview:tableView];
        
        _rightTableView = tableView;
        
        tableView.dataSource = self;
        tableView.delegate = self;
        
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:rightCellIdentifier];
        tableView.backgroundColor = [UIColor cyanColor];
        tableView.tableFooterView = [[UIView alloc] init];
        
    }
    return _rightTableView;
}
@end
```

相关文章

网友评论

  • 68f63b315167:用两个相关联接口时,右边列表的网络数据请求怎么做
  • 厚脸皮小强打不死的小强:大哥,如果增加一个头部,跟随tableView一起滑动,这个怎么做
  • alantan:赞赞赞
    alantan:左边leftTableView 的选中方法乱跳,而且选中的结果会出错,这怎么整
  • 翻滚的炒勺2013:写的真好
  • 天真烂漫的孩子:貌似好多人懒加载strong属性的tableView,在Plus模拟器上会奔溃,其它模拟器不会.亲测确实Plus会奔溃,其它模拟器不会,大神知道怎么回事么?
  • 独木舟的木:你好,我使用了你的方法在自己的APP上发现TableView的位置有点偏移
    左侧tableView的
    ``CGRect leftTableRect = CGRectMake(0, 0, leftTableWidth, screenHeight);``
    是正常显示的的。
    右侧tableView的
    ``CGRect rightTableRect = CGRectMake(leftTableWidth, 64, rightTableWidth, screenHeight);``
    它的Y坐标要设置成64的时候,才是和左侧的tableView水平对齐的。
    请问这是什么原因呢?
    天真烂漫的孩子:self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.leftTableView];
    [self.view addSubview:self.rightTableView];
    haoAku:automaticallyAdjustsScrollViewInsets = NO; // 自动滚动调整,默认为YES 这样我们的内容就不会自动偏移了
  • feng_dev:左边是一列按钮,不用table,右边是几个 tableView,点击对应按钮,让对应的tableView显示到最上面,右边刷新数据,怎么弄?
    代理方法里面 是根据 按钮 来判断,还是根据tableView来判断比较好?
  • 善良的皮蛋:我也出现了6楼的问题,楼主,可以帮忙看看?
  • DamonYJ:👍👍👍
  • 3a93b6ca03e3:我想请问下,后台传回数据,如果有左边的标题,但是右边的数组长度是0的,那点击这个左边的标题,会崩,这个应该怎么解决呢
    3a93b6ca03e3:@烏先森 找到解决方法了,NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];把这个0改为NSNotFound就可以了,避免右边tableview只有标题没有数据时奔溃问题 :smile:
  • C_HPY:赞,学习了。
  • YYYLynn:在左边的tableView关联右边的tableView的 didSelectRowAtIndexPath方法里面, 控制右边的tableView滚动的时候,也会走scrollViewDidScroll: 方法, 就会再走一遍左边的tableView选中的方法, 使得点击左边的tableView的cell时,会出现闪动,没有那么流畅啊~~,我看到一个demo是用:- (void)tableView:(UITableView *)tableView willDisplayHeaderView:
    - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView: 这两个方法的,联动好像也没有不准啊, 具体的作者能麻烦说说嘛??
    沉默的鱼sunny:@旁蟹棒 好像加上也不行啊,有什么方法吗
    旁蟹棒:@YYYLynn if(scrollView == self.leftTableView || !self.rightTableView.dragging){...} 加个判断就可了
    3a93b6ca03e3:@YYYLynn 我也出现这个问题,点击左边会走两次选中
  • JasonEVA:赞一个,有个问题
    // 将右侧 tableView 移动到指定位置
    [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

    // 取消选中效果
    [self.rightTableView deselectRowAtIndexPath:moveToIndexPath animated:YES];
    点击左侧tableview定位右侧tableview的方法用这两个方法是不是没什么必要?还是另有用意?我觉得用 [self.rightTableView scrollToRowAtIndexPath:moveIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    就可以了
    o惜乐o:其实是有区别的,一,[self.rightTableView scrollToRowAtIndexPath:moveIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];这个方法主要是是滚到指定索引位置,而原来选中的状态没有清除,二.楼主的两个方法也能滚动到指定索引,但是他的是选中当前的索引,又取消当前的索引,所以rightTableView不存在被选中的cell.根据不同需求而定吧.
    haoAku:@JasonEVA 你好谢谢你的反馈,当时没注意到这个 api .确实,这个方法可以取代那两个方法,我这就把代码改一下 :+1:
  • Idoahc:直接点击左边tableView的最后一行的时候,选中状态会自动跳到倒数第二行,可以再修改下,点击左边最后一个的时候,让右边对应的区头就显示到最上面,假如是最后已个区的时候,也是一样的,就让右边cell的下面为空白就好
    不泯iOS:@SuperDi 所以想问下怎么实现你说的那种效果(点击最后一个cell的时候,右边的cell下面是空白的),因为看着不爽:grin:
    Idoahc:@sunshineAku 恩,你这考虑也是对的,我的想法就是有点强迫症 :joy:
    haoAku:@JasonMax 你好,谢谢你的建议! 是因为最后一组 cell 的总高度低于 tableView 的高度的情况下,才会出现此现象.
    而让用户点击左侧 TableView 的目的是为了能快速的让右侧的 tableView 对应的数据呈现给用户
    如果只是为了单纯的将显示样式统一,将剩余 cell 用空白填补,会让用户感觉商品列表不齐全,从而影响到app整体美观
    这是我个人的看法!不知道你是怎么认为的...
  • yyMae:思路清晰简单,赞一个
    haoAku:@yyMae 谢谢!
  • Kenter_Huang::+1:
    Kenter_Huang:@sunshineAku 嗯嗯 都是英文看的头痛
    haoAku:@金台 谢谢! [self.rightTableView indexPathsForVisibleRows] 这个方法我也是第一次用,多看 apple API 吧,他能给你许多惊喜 :yum:

本文标题:iOS两个 TableView 联动. 思路简单明了,无bug!

本文链接:https://www.haomeiwen.com/subject/kofwsttx.html