1.实现方式
1.1 点左边的时候实现右边的tableview滚动到相应的section:
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
1.2 右边的tableview滚到的时候左边的跟着动动,主要的思维就是拿到右边的tableview的visibleCells,再取出第一个cell,根据cell取出indexPath。做项目的时候有个小插曲就是cell会取不到正确的位置,后来排查是因为frame值的位数太小,0.00005之类的导致的,后来直接取整.
2.GitHub链接:https://github.com/hehuachaoKevin/TwoTableViewConnected
3.代码
//// LeftTableView.m// TwoTableViewConnected//// Created by KevinHe on 2017/12/21.// Copyright © 2017年 KevinHehuachao. All rights reserved.//#import "LeftTableView.h"@interface LeftTableView ()@property (nonatomic,assign)NSInteger currentSelectedIndex;
@end
@implementation LeftTableView
- (instancetype)init
{
self = [super init];
if (self) {
[self createUI];
[self initData];
}
return self;
}
- (void)initData{
self.currentSelectedIndex = 0;
}
- (void)refreshData:(NSArray*)dataArray{
[self.dataSource removeAllObjects];
[self.tableView reloadData];
[self setSeletedIndex:self.currentSelectedIndex];
}
- (void)createUI{
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*3/10.0, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
tableView.showsVerticalScrollIndicator = NO;
tableView.bounces = NO;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"LeftTableViewUITableViewCell"];
tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*3/10.0, CGFLOAT_MIN)];
self.tableView = tableView;
[self addSubview:self.tableView];
self.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
self.tableView.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
}
- (void)setSeletedIndex:(NSInteger)index{
if (self.currentSelectedIndex == index) {
return;
}
self.currentSelectedIndex = index;
[self.tableView reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
if (self.dataSource.count>index) {
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
}
- (NSInteger)currentSeletedIndex{
return self.tableView.indexPathForSelectedRow.row;
}
#pragma mark - tableView.dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftTableViewUITableViewCell"];
if (self.currentSelectedIndex == indexPath.row) {
cell.backgroundColor = [UIColor whiteColor];
}
else{
cell.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
return cell;
}
#pragma mark - delegate.tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.currentSelectedIndex = indexPath.row;
[self.tableView reloadData];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self setCellSelected:cell];
NSIndexPath *indexPathTemp = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
if ([self.delegate respondsToSelector:@selector(setCurrentIndexPath:)]) {
[self.delegate setCurrentIndexPath:indexPathTemp];
}
}
- (void)setCellSelected:(UITableViewCell *)cell{
if (cell) {
cell.backgroundColor = [UIColor whiteColor];
}
}
//// ViewController.m// TwoTableViewConnected//// Created by KevinHe on 2017/12/21.// Copyright © 2017年 KevinHehuachao. All rights reserved.//#import "ViewController.h"#import "LeftTableView.h"@interface ViewController ()@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,weak)LeftTableView *leftTableView;
@property (nonatomic,assign)BOOL leftTableViewNeedScroll;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
[self initData];
}
- (void)initData{
//注意 要初始化
self.leftTableViewNeedScroll = YES;
}
- (void)createUI{
//左边视图
LeftTableView *leftTableView = [[LeftTableView alloc]init];
[self.view addSubview:leftTableView];
_leftTableView = leftTableView;
_leftTableView.frame = CGRectMake(0, 64, SCREEN_WIDTH*3/10, SCREEN_HEIGHT-64);
self.leftTableView.delegate = self;
self.leftTableView.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
//tableView
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*3/10.0, 64, SCREEN_WIDTH*7/10.0, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
tableView.showsVerticalScrollIndicator = NO;
tableView.bounces = NO;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
tableView.backgroundColor = [UIColor grayColor];
self.tableView = tableView;
[self.view addSubview:self.tableView];
//ios 11 以上的问题
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
}
#pragma mark - scrollView.delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self getCurrentSection];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
self.leftTableViewNeedScroll = YES;
}
//调整左边的tableview
- (void)getCurrentSection{
NSArray *array = [self.tableView visibleCells];
if (array.count) {
UITableViewCell *cell = [array objectAtIndex:0];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (indexPath) {
if ([self.leftTableView currentSeletedIndex]!=indexPath.section
&& self.leftTableViewNeedScroll) {
[self.leftTableView setSeletedIndex:indexPath.section];
}
}
}
}
#pragma mark - delegate.LeftTableView
- (void)setCurrentIndexPath:(NSIndexPath*)indexPath{
self.leftTableViewNeedScroll = NO;
NSArray *array = [self.tableView visibleCells];
if (array.count) {
UITableViewCell *cell = [array objectAtIndex:0];
NSIndexPath *indexPathTemp = [self.tableView indexPathForCell:cell];
if (indexPath) {
if (indexPathTemp.section!=indexPath.section) {
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}
}
}
}
#pragma mark - tableView.delegate
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(0, 0, SCREEN_WIDTH, 36);
label.text = [NSString stringWithFormat:@"%ld",(long)section];
return label;
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
return 36;
}
#pragma mark - tableView.dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
cell.textLabel.text = [NSString stringWithFormat:@"section=%ld row=%ld",indexPath.section,indexPath.row];
return cell;
}
#pragma mark - 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}