IOS实现左右两个TableView联动效果

一、先来看看要实现的效果图

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

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

1、点击 左侧 cell 让右侧 tableView 滚到对应位置

2、滑动 右侧 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];

  // 将右侧 tableView 移动到指定位置
  [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

  // 取消选中效果
  [self.rightTableView deselectRowAtIndexPath:moveToIndexPath 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 已经显示
// 这两个方法都不准确
}

五、以下是所有示例代码

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

六、总结

IOS实现左右两个TableView联动效果的内容到这就结束了,这种的效果在我们平常的时候还是挺常见的,感兴趣的朋友们可以自己动手操作起来,希望对大家的学习工作能有所帮助。

(0)

相关推荐

  • 详解iOS开发中UItableview控件的数据刷新功能的实现

    实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运行界面: 点击选中行: 修改数据后自动刷新: 三.代码示例 数据模型部分: YYheros.h文件 复制代码 代码如下: // //  YYheros.h //  10-英雄展示(数据刷新) // //  Created by apple on 14-5-29. //  Copyright (c) 2014年 itc

  • 详解iOS开发中UITableview cell 顶部空白的多种设置方法

    我知道没人会主动设置这个东西,但是大家一定都遇到过这个问题,下面总结下可能是哪些情况: 1, self.automaticallyAdjustsScrollViewInsets = NO; 这个应该是最常见而且不容易被发现的原因,起因是iOS7在Conttoller中新增了automaticallyAdjustsScrollViewInsets这个属性,当设置为YES时(默认YES),如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scr

  • iOS应用中UITableView左滑自定义选项及批量删除的实现

    实现UITableView左滑自定义选项 当UITableView进入编辑模式,在进行左滑操作的cell的右边,默认会出现Delete按钮,如何自定义左滑出现的按钮呢? 只需要实现UITableView下面的这个代理方法. 复制代码 代码如下: - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

  • iOS开发之UITableView左滑删除等自定义功能

    前言 相信每位iOS开发者都知道UITableView的左滑删除功能非常的炫酷,有时候左滑需要的功能不止只有删除一个,有时候会有顶置之类的别的功能,这时候就需要我们自己定制左滑 示例代码 -(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *rowActio

  • iOS 中使用tableView实现右滑显示选择功能

    1.在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只要一个代理方法和一个类就行了 2.iOS8的协议对了一个方法,返回值是数组的tableview:editActionForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableviewRowAction 3.在UITableviewRowAction类.我们可以设置按钮的样式,显示文字.背景色和按钮事

  • iOS App中UITableView左滑出现删除按钮及其cell的重用

    UITableView的编辑模式 实现UITableView简单的删除功能(左滑出现删除按钮) 首先UITableView需要进入编辑模式.实现下面的方法,即使什么代码也不写也会进入编辑模式: 复制代码 代码如下: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)index

  • 改变iOS应用中UITableView的背景颜色与背景图片的方法

    改变UITableView的header.footer背景颜色 改变UITableView的header.footer背景颜色,这是个很常见的问题.之前知道的一般做法是,通过实现tableView: viewForHeaderInSection:返回一个自定义的View,里面什么都不填,只设背景颜色.但是今天发现一个更简洁的做法: 对于iOS 6及以后的系统,实现这个新的delegate函数即可: 复制代码 代码如下: - (void)tableView:(UITableView *)table

  • iOS开发之tableView实现左滑删除功能

    前言 这几天要实现左划删除的功能,发现网上很多帖子大多出自一人之手,然后都是 copy 的文章,其实都没有那么复杂,只实现一个代理方法就可以了 方法如下 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITa

  • iOS应用开发中UITableView的分割线的一些设置技巧

    对于ios7,ios8及以上来说,调整UITableView的cell的分割线位置已经是相当不便,因为UITableView内部使用了margin layout. 其实只需要如下这样子就可以实现分割线的控制. 复制代码 代码如下: -(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath {     // 下面

  • 全面解析iOS应用中自定义UITableViewCell的方法

    有时候我们需要自己定义UITableViewCell的风格,其实就是向行中添加子视图.添加子视图的方法主要有两种:使用代码以及从.xib文件加载.当然后一种方法比较直观. 一.基本用法 我们这次要自定义一个Cell,使得它像QQ好友列表的一行一样:左边是一张图片,图片的右边是三行标签: 当然,我们不会搞得这么复杂,只是有点意思就行. 1.运行Xcode 4.2,新建一个Single View Application,名称为Custom Cell: 2.将图片资源导入到工程.为此,我找了14张50

随机推荐