tableView左右联动

实现 tableView联动 主要有两个细节(需要创建两个tableView)

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

//MARK: - 点击 cell 的代理方法
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
// 判断是否为 左侧 的 tableView
if(tableView ==self.leftTableView) {
// 计算出 右侧 tableView 将要 滚动的 位置
NSIndexPath*moveToIndexPath = [NSIndexPathindexPathForRow:0inSection:indexPath.row];
// 将 rightTableView 移动到对应的 位置
[self.rightTableView scrollToRowAtIndexPath:moveToIndexPath atScrollPosition:UITableViewScrollPositionTopanimated:YES]; }}

2、滑动右侧 tableView 让左侧 tableView 滚到对应位置

//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];
}

参考代码:

#import "ViewController.h"
#import "food.h"
#import "foodGroup.h"
#import "LeftTableViewCell.h"
#import "RightTableViewCell.h"

#import "UIImageView+WebCache.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>
{
    //数据源
    NSMutableArray *_dataSource;
    
}
@property (nonatomic, weak) UITableView *leftTableView;

@property (nonatomic, weak) UITableView *rightTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSource = [[NSMutableArray alloc]init];
    
    [self.view addSubview:_leftTableView];
    [self.view addSubview:_rightTableView];
    
    [self createleftTableView];
    [self createRightTableView];
    //获取数据源
    [self getDataSource];
    
}
- (void)createleftTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, leftTableWidth, ScreenHeight-64)];
    [self.view addSubview:tableView];
    _leftTableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[LeftTableViewCell class] forCellReuseIdentifier:leftCellIdentifier];
    
}
- (void)createRightTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(leftTableWidth, 64, rightTableWidth, ScreenHeight-64)];
    [self.view addSubview:tableView];
    _rightTableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    _rightTableView.rowHeight = 80;
    [tableView registerNib:[UINib nibWithNibName:@"RightTableViewCell" bundle:nil] forCellReuseIdentifier:rightCellIdentifier];
}



//获取数据源
- (void)getDataSource{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"meituan" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSArray *foodGroups = dict[@"data"][@"food_spu_tags"];
    
    for (NSDictionary *foodGroupsDic in foodGroups) {
        foodGroup *MyGroup = [[foodGroup alloc]init];
        MyGroup.name = foodGroupsDic[@"name"];
        NSArray * foodArray = foodGroupsDic[@"spus"];
       
        for (NSDictionary *foodDic in foodArray) {
            food *MyFood = [[food alloc]init];
            MyFood.name = foodDic[@"name"];
            MyFood.ImageURL = foodDic[@"picture"];
            MyFood.month_saled_content = foodDic[@"month_saled_content"];
            [MyGroup.foodArray addObject:MyFood];
        }
        
        [_dataSource addObject:MyGroup];
    
    }
    
    [_leftTableView reloadData];
    [_rightTableView reloadData];
}

#pragma mark - 数据源
//返回指定的组数,如果是多组,必须实现这个方法,返回指定组数,如果是一组,这个方法可以不实现,默认是一组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == _leftTableView) {
        return 1;
    }
    return _dataSource.count;
}
//返回指定组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _leftTableView) {
        return _dataSource.count;
    }else{
        foodGroup *Group = _dataSource[section];
        return Group.foodArray.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == _leftTableView) {
        //左边cell
        LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftCellIdentifier forIndexPath:indexPath];
        foodGroup *Group = _dataSource[indexPath.row];
        cell.textLabel.text = Group.name;
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
        
        return cell;
    }else{
        //右边cell
        RightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rightCellIdentifier forIndexPath:indexPath];
        foodGroup *Group = _dataSource[indexPath.section];
        food *MyFood = Group.foodArray[indexPath.row];
        
        cell.foodNameLabel.text = MyFood.name;
        cell.xiaoLiangLAbel.text = MyFood.month_saled_content;
        [cell.MYHeadImageView sd_setImageWithURL:[NSURL URLWithString:MyFood.ImageURL]];
        
        return cell;
    }
    
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if (tableView == _rightTableView) {
        foodGroup *Group = _dataSource[section];
        return Group.name;
    }else{
        return nil;
    }
}

#pragma mark - 滑动 右侧 tableView 让左侧 tableView 滚到对应位置
//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];
    }
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。