我這里說的聯(lián)動的意思就是操作AtableView 讓BtableView滾動到相應(yīng)的位置,操作BtableView讓AtableView滾動到相應(yīng)的位置.
先給個參考圖看一下好說.

首先介紹一下這個結(jié)構(gòu).
首先左邊的tableView是一個控制 leftViewController
右邊的是一個控制器rightViewController
右邊控制器rightViewController的rightTableView加到了左邊 控制器的View上了(用到了addChildViewController)
在左邊的控制器創(chuàng)建右邊控制器 這就拿到了 右邊控制器的 引用 在右邊控制器中寫個 方法 點擊左邊 用右邊的 引用直接調(diào)用 方法移動 就好了
移動右邊 讓左邊移動,在右控制器邊同樣的拿到左邊的 引用吧 ,用代理.....
還是看代碼吧
ViewController不重要只是加一個導(dǎo)航
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
#import "leftViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *bution = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
bution.center = self.view.center;
bution.backgroundColor = [UIColor redColor];
[bution addTarget:self action:@selector(butionCleck:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bution];
}
-(void)butionCleck:(UIButton *)sender{
leftViewController *leftVC = [leftViewController new];
[self.navigationController pushViewController:leftVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
leftViewController.h
#import <UIKit/UIKit.h>
@interface leftViewController : UIViewController
@end
leftViewController.m
#import "leftViewController.h"
#import "rightViewController.h"
@interface leftViewController ()<UITableViewDataSource,UITableViewDelegate,rightViewControllerDeldgate>
@property(nonatomic,strong)UITableView *leftTableView;
@property(nonatomic,strong)NSArray *leftDataArr;
@property(nonatomic,strong)rightViewController *rightVC;
@end
@implementation leftViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.leftTableView];
[self creatRightVC];
}
-(void)creatRightVC{
self.rightVC = [rightViewController new];
self.rightVC.delegate = self;
[self addChildViewController:_rightVC];
[self.view addSubview:_rightVC.view];
}
-(UITableView *)leftTableView{
if (!_leftTableView) {
_leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 120, self.view.frame.size.height) style:UITableViewStylePlain];
_leftTableView.showsVerticalScrollIndicator = NO;
_leftTableView.delegate = self;
_leftTableView.dataSource = self;
[_leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell11"];
}
return _leftTableView;
}
-(NSArray *)leftDataArr{
if (!_leftDataArr) {
_leftDataArr = @[@"第一類",
@"第二類",
@"第三類",
@"第四類",
@"第五類",
@"第六類",
@"第七類",
@"第八類",
@"第九類",
@"第十類",
@"第十一類",
@"第十二類",
@"第十三類",
@"第十四類",
@"第十五類",
@"第十六類",
@"第十七類",
@"第十八類",
@"第十九類",
@"第二十類",
@"第二十一類"
];
}
return _leftDataArr;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.leftDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell11" forIndexPath:indexPath];
cell.textLabel.text = self.leftDataArr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"-----%ld",indexPath.row);
if (self.rightVC) {
[self.rightVC scrollToSelectIndexPath:indexPath];
}
}
/*
代理右邊選擇時執(zhí)行代理.
*/
-(void)rightViewControllerDelegate:(rightViewController *)vc withIndexPatch:(NSIndexPath *)ptch{
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:ptch.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
-(void)rightViewControllerDelegateHeaderViewAppear:(rightViewController*)vc withIndexPatch:(NSInteger)section{
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
-(void)rightViewControllerDelegateHeaderViewdisappear:(rightViewController *)vc withIndexPatch:(NSInteger)section{
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
rightViewController.h
#import <UIKit/UIKit.h>
@class rightViewController;
@protocol rightViewControllerDeldgate <NSObject>
-(void)rightViewControllerDelegate:(rightViewController *)vc withIndexPatch:(NSIndexPath *)ptch;
-(void)rightViewControllerDelegateHeaderViewAppear:(rightViewController *)vc withIndexPatch:(NSInteger )section;
-(void)rightViewControllerDelegateHeaderViewdisappear:(rightViewController *)vc withIndexPatch:(NSInteger )section;
@end
@interface rightViewController : UIViewController
@property(nonatomic,weak)id <rightViewControllerDeldgate> delegate;
-(void)scrollToSelectIndexPath:(NSIndexPath *)path;
@end
rightViewController.m
#import "rightViewController.h"
@interface rightViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *rightTableView;
@property(nonatomic,strong)NSArray *rightDataArr;
@property(nonatomic,assign)BOOL isScrollUp;
@property(nonatomic, assign)CGFloat lastOffsetY;//滾動即將結(jié)束時scrollView的偏移量
@property(nonatomic,assign)BOOL ifScroll; //當左邊cell點擊-> 右邊滑動-> 右邊滑動頭消失頭出現(xiàn)就會執(zhí)行代理->代理方法再讓左邊滑動 給個boll值設(shè)置為NO就是為了 點擊左邊cell右邊頭出現(xiàn)或者消失 都不執(zhí)行代理方法 左邊也不會滑動.
@end
@implementation rightViewController
-(UITableView *)rightTableView{
if (!_rightTableView) {
_rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_rightTableView.showsVerticalScrollIndicator = NO;
_rightTableView.delegate = self;
_rightTableView.dataSource =self;
[_rightTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[_rightTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headerView"];
}
return _rightTableView;
}
-(NSArray *)rightDataArr{
if (!_rightDataArr) {
_rightDataArr =
@[
@[@"1橘子",@"西瓜",@"哈密瓜",@"蘋 果",@"柚子",@"香蕉"],
@[@"2華為手機",@"蘋果5",@"蘋果5s",@"蘋果6",@"蘋果6+",@"蘋果6s",@"蘋果6s+"],
@[@"3聯(lián)想電腦",@"華碩電腦",@"MAC"],
@[@"4雞肉",@"鴨肉",@"牛肉",@"豬肉",@"羊肉"],
@[@"5香菇",@"蘑菇",@"大頭菜",@"青椒"],
@[@"6花生油",@"大豆油",@"葵花籽油",@"芝 麻香油"],
@[@"7花椒",@"陳皮",@"八角"],
@[@"8橘子",@"西瓜",@"哈密瓜",@"蘋果",@"柚子",@"香蕉"],
@[@"9華為手機",@"蘋果5",@"蘋果5s",@"蘋果6",@"蘋果6+",@"蘋果6s",@"蘋果6s+"],
@[@"10聯(lián)想電腦",@"華碩電腦",@"MAC"],
@[@"11雞肉",@"鴨肉",@"牛肉",@"豬肉",@"羊肉"],
@[@"12香菇",@"蘑菇",@"大頭菜",@"青椒"],
@[@"13花生油",@"大豆油",@"葵花籽油",@"芝麻香油"],
@[@"14花椒",@"陳皮",@"八角"],
@[@"15橘子",@"西瓜",@"哈密瓜",@"蘋果",@"柚子",@"香蕉"],
@[@"16華為手機",@"蘋果5",@"蘋果5s",@"蘋果6",@"蘋果6+",@"蘋果6s",@"蘋果6s+"],
@[@"17聯(lián)想電腦",@"華碩電腦",@"MAC"],
@[@"18雞肉",@"鴨肉",@"牛肉",@"豬肉",@"羊肉"],
@[@"19香菇",@"蘑菇",@"大頭菜",@"青椒"],
@[@"20花生油",@"大豆油",@"葵花籽油",@"芝麻香油"],
@[@"21花椒",@"陳皮",@"八角"]
];
}
return _rightDataArr;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.rightDataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *arr = self.rightDataArr[section];
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSArray * data = self.rightDataArr[indexPath.section];
cell.textLabel.text = data[indexPath.row];
return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
headerView.textLabel.text = [NSString stringWithFormat:@"第%ld區(qū)",section+1];
return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.frame = CGRectMake(120, 64,self.view.frame.size.width-120, self.view.frame.size.height-64);
self.view.backgroundColor = [UIColor redColor];
[self.view addSubview:self.rightTableView];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (_delegate && [_delegate respondsToSelector:@selector(rightViewControllerDelegate:withIndexPatch:)]) {
[_delegate rightViewControllerDelegate:self withIndexPatch:indexPath];
}
}
//頭將出現(xiàn)
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
if (_delegate && [self.delegate respondsToSelector:@selector(rightViewControllerDelegateHeaderViewAppear:withIndexPatch:)] && !_isScrollUp && _ifScroll) {
[_delegate rightViewControllerDelegateHeaderViewAppear:self withIndexPatch:section];
}
}
//頭講消失
-(void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section{
if (_delegate && [self.delegate respondsToSelector:@selector(rightViewControllerDelegateHeaderViewdisappear:withIndexPatch:)] && _isScrollUp &&_ifScroll) {
//上滑才執(zhí)行
[_delegate rightViewControllerDelegateHeaderViewdisappear:self withIndexPatch:section+1];
}
}
//主要判斷向上滑動還是向下滑動
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
_isScrollUp = _lastOffsetY < scrollView.contentOffset.y;
_lastOffsetY = scrollView.contentOffset.y;
//isDragging Dragging 拖拉
if (scrollView.isDragging) {
_ifScroll = YES;
}
}
//滾動到制定的NSIndexPath
-(void)scrollToSelectIndexPath:(NSIndexPath *)path{
NSLog(@"%ld",path.row);
_ifScroll = NO;
[self.rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:path.row] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
這篇是我看例子http://www.itdecent.cn/p/c118a29887ca
由于有點小問題就是 (慢慢拖著拖著不放)滑動右邊 左邊的聯(lián)動有時不會出現(xiàn)
首先考慮一個問題
當我們 操作左邊tableView的時候 讓右邊動.
我們動右邊的時候 讓左邊動.
比如說 我操作左邊 右邊動了 (右邊一動左邊是不是受到影響呢?)
分析一下我們 想要的結(jié)果是
我操作左邊 讓右邊東(這個時候不想讓左邊受影響)
我拖動右邊時才讓左邊受影響.
那么我們只需 判斷出 右邊的動 是我操作左邊他才動的 還是我直接拖動右邊才動的.
所有的解釋都在 代碼注釋里.
效果圖如圖

