ios实现两个tableview联动

该博客介绍了如何在iOS应用中实现两个TableView的联动效果。通过监听左侧TableView的滚动,利用`tableView:willDisplayHeaderView:forSection:`方法更新右侧TableView的选中状态,达到同步显示的效果。内容包括初始化数据源、设置TableView属性、实现DataSource和Delegate方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个tableview的联动,滑动左侧tableview,右侧tableview跟着滑动

其实实现起来比较简单,只是需要搞清楚他们之间的区别和联系,还有就是调用一个

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

这个方法,从而实现左右两个tableview的联动

@implementation ViewController

{

    UITableView *_rightTableView;

    UITableView *_leftTableView;

    NSArray *_leftTableSource;

    NSArray *_rightTableSource;

    

}

初始化数据源

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _leftTableSource = @[@"11",@"22",@"33",@"44",@"55",@"66"];

    _rightTableSource = @[@{@"header":@"11",@"title":@[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff"]},

        @{@"header":@"22",@"title":@[@"gg",@"mm",@"nn",@"oo",@"pp",@"qq"]},

        @{@"header":@"33",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]},

        @{@"header":@"44",@"title":@[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff"]},

        @{@"header":@"55",@"title":@[@"gg",@"mm",@"nn",@"oo",@"pp",@"qq"]},

        @{@"header":@"66",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]}];

    

    [self setupSomeParamars];

}

创建两个tableview

- (void)setupSomeParamars

{

    _rightTableView = [[UITableView allocinitWithFrame:CGRectMake(1000self.view.frame.size.width - 100self.view.frame.size.heightstyle:UITableViewStyleGrouped];

    _rightTableView.dataSource = self;

    _rightTableView.delegate = self;

    [self.view addSubview:_rightTableView];

    

    _leftTableView = [[UITableView allocinitWithFrame:CGRectMake(00100self.view.frame.size.height)style:UITableViewStyleGrouped];

    _leftTableView.dataSource = self;

    _leftTableView.delegate = self;

    [self.view addSubview:_leftTableView];

    

}

设置cell的显示

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

{

    static NSString *reuseIdentifer = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];

    if(!cell){

        cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer];

    }

    if(tableView == _rightTableView){

        cell.textLabel.text = [_rightTableSource[indexPath.sectionobjectForKey:@"title"][indexPath.row];

    }else if (tableView == _leftTableView){

        cell.textLabel.text = _leftTableSource[indexPath.row];

    }

    return cell;

    

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    if (tableView == _rightTableView) {

        return 50;

    }else{

        return 50;

    }

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    if (tableView == _rightTableView) {

        return _rightTableSource.count;

    }else{

        return 1;

    }

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if (tableView == _leftTableView) {

        return _leftTableSource.count;

    }else{

        return [[_rightTableSource[section] objectForKey:@"title"count];

    }

}


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

{

    if (tableView == _rightTableView) {

        return [_rightTableSource[section] objectForKey:@"header"];

    }else{

        return nil;

    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    if(tableView == _rightTableView){

        UILabel *label = [[UILabel allocinitWithFrame:CGRectMake(00self.view.frame.size.width-10040)];

        label.backgroundColor = [UIColor cyanColor];

        label.text = [_rightTableSource[section] objectForKey:@"header"];

        label.textColor = [UIColor redColor];

        return label;

    }else{

        return nil;

    }

}


联动效果在于这里

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

{

    if(tableView == _rightTableView){

        [_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:section inSection:0animated:YESscrollPosition:UITableViewScrollPositionNone];

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值