iOS之tableView首页投稿(暂停使用,暂停投稿)iOS Developer

UITableView的联动效果 | 二

2016-11-23  本文已影响240人  JoyceZhao

之前写过一篇纯代码实现UITableView的联动效果的文章,查看地址:http://www.jianshu.com/p/15625efdd146
接下来给大家介绍利用storyboard实现UITableView的联动效果,分以下三部分进行介绍

界面搭建

创建新的XCode项目,在搭建如下图所示的界面:一左一右两个UITableView

界面搭建.png

运行结果

UITableView的联动效果如下图所示

运行结果图.gif

代码实现

1 主要实现方法

1.1 点击cell的方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

1.2 滚动右侧UITableView的方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

2 具体代码

//
//  ViewController.m
//  TwoTablesViewStroryboardDemo
//
//  Created by Joyce on 16/11/23.
//  Copyright © 2016年 Joyce. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark ------------------
#pragma mark - 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == _leftTableView) {
        return 40;
    }
    return 8;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (tableView == _leftTableView) {
        return 1;
    }
    return 40;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (tableView == _rightTableView) {
        return [NSString stringWithFormat:@"第%ld组", section];
    }
    return nil;
}

#pragma mark ------------------
#pragma mark - 代理方法
// cell显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] init];
    if (tableView == _leftTableView) {
        cell.backgroundColor = [UIColor cyanColor];
        cell.textLabel.text = [NSString stringWithFormat:@"第%ld组", indexPath.row];
        return cell;
    } else if (tableView == _rightTableView) {
        cell.backgroundColor = [UIColor purpleColor];
        cell.textLabel.text = [NSString stringWithFormat:@"第%ld组,第%ld行", indexPath.section, indexPath.row];
        return cell;
    }
    return nil;
}

// cell点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _leftTableView) {
        
        [_rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];
        
    } else {
        
        [_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
        
    }
    
}

// 右侧tableView的滚动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView == _rightTableView) {
        [_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[[_rightTableView indexPathsForVisibleRows] firstObject].section inSection:0] animated:YES scrollPosition:(UITableViewScrollPositionTop)];
    }
}

@end

结束语
仅供学习参考,更多复杂功能还需小伙伴们亲自动手实现,记得分享,互相学习😯。

上一篇下一篇

猜你喜欢

热点阅读