我这里说的联动的意思就是操作AtableView 让BtableView滚动到相应的位置,操作BtableView让AtableView滚动到相应的位置.
先给个参考图看一下好说.
首先介绍一下这个结构.
首先左边的tableView是一个控制 leftViewController
右边的是一个控制器rightViewController
右边控制器rightViewController的rightTableView加到了左边 控制器的View上了(用到了addChildViewController)
在左边的控制器创建右边控制器 这就拿到了 右边控制器的 引用 在右边控制器中写个 方法 点击左边 用右边的 引用直接调用 方法移动 就好了
移动右边 让左边移动,在右控制器边同样的拿到左边的 引用吧 ,用代理.....
还是看代码吧
ViewController不重要只是加一个导航
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];
}
}
/*
代理右边选择时执行代理.
*/
-(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;//滚动即将结束时scrollView的偏移量
@property(nonatomic,assign)BOOL ifScroll; //当左边cell点击-> 右边滑动-> 右边滑动头消失头出现就会执行代理->代理方法再让左边滑动 给个boll值设置为NO就是为了 点击左边cell右边头出现或者消失 都不执行代理方法 左边也不会滑动.
@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联想电脑",@"华硕电脑",@"MAC"],
@[@"4鸡肉",@"鸭肉",@"牛肉",@"猪肉",@"羊肉"],
@[@"5香菇",@"蘑菇",@"大头菜",@"青椒"],
@[@"6花生油",@"大豆油",@"葵花籽油",@"芝 麻香油"],
@[@"7花椒",@"陈皮",@"八角"],
@[@"8橘子",@"西瓜",@"哈密瓜",@"苹果",@"柚子",@"香蕉"],
@[@"9华为手机",@"苹果5",@"苹果5s",@"苹果6",@"苹果6+",@"苹果6s",@"苹果6s+"],
@[@"10联想电脑",@"华硕电脑",@"MAC"],
@[@"11鸡肉",@"鸭肉",@"牛肉",@"猪肉",@"羊肉"],
@[@"12香菇",@"蘑菇",@"大头菜",@"青椒"],
@[@"13花生油",@"大豆油",@"葵花籽油",@"芝麻香油"],
@[@"14花椒",@"陈皮",@"八角"],
@[@"15橘子",@"西瓜",@"哈密瓜",@"苹果",@"柚子",@"香蕉"],
@[@"16华为手机",@"苹果5",@"苹果5s",@"苹果6",@"苹果6+",@"苹果6s",@"苹果6s+"],
@[@"17联想电脑",@"华硕电脑",@"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区",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];
}
}
//头将出现
-(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) {
//上滑才执行
[_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.jianshu.com/p/c118a29887ca
由于有点小问题就是 (慢慢拖着拖着不放)滑动右边 左边的联动有时不会出现
首先考虑一个问题
当我们 操作左边tableView的时候 让右边动.
我们动右边的时候 让左边动.
比如说 我操作左边 右边动了 (右边一动左边是不是受到影响呢?)
分析一下我们 想要的结果是
我操作左边 让右边东(这个时候不想让左边受影响)
我拖动右边时才让左边受影响.
那么我们只需 判断出 右边的动 是我操作左边他才动的 还是我直接拖动右边才动的.
所有的解释都在 代码注释里.
效果图如图