IOS Tableview 联动

这篇博客介绍了如何在iOS应用中创建并实现两个TableView的联动效果。通过定义两个TableView,分别设置其代理和数据源,实现不同TableView间的点击和滑动交互,确保当一个TableView滚动或选择时,另一个TableView会相应地滚动到对应的位置。

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

// 创建两个tablview

#import "ViewController.h"

#define SCREEN_WIDTH self.view.frame.size.width

#define SCREEN_HEIGHT self.view.frame.size.height

 

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    NSArray *Proarr;

    NSArray *cityAtt;

}

 

@property (nonatomic,strong)UITableView *LeftTable;

@property (nonatomic,strong)UITableView *RightTable;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    Proarr =@[@"河北",@"河南",@"吉林",@"山东",@"山西"];

cityAtt=@[@[@"1",@"2",@"2",@"2",@"2",@"2",@"2"],@[@"2",@"2"],@[@"3",@"3",@"3"],@[@"4",@"4",@"4",@"4"],@[@"5",@"5",@"5"@"5",@"5"]];

    [self.view addSubview:self.LeftTable];

    [self.view addSubview:self.RightTable];

}

-(UITableView *)LeftTable

{

    if (!_LeftTable)

    {

        _LeftTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH-100 , SCREEN_HEIGHT) style:UITableViewStyleGrouped];

        _LeftTable.tag = 100;

        _LeftTable.delegate = self;

        _LeftTable.dataSource = self;

    }

    return _LeftTable;

}

-(UITableView *)RightTable

{

    if (!_RightTable) {

        _RightTable = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, (SCREEN_HEIGHT-150)/2,150 , 30*5) style:UITableViewStylePlain];

        _RightTable.rowHeight = 30;

        _RightTable.tag = 110;

        _RightTable.delegate = self;

        _RightTable.dataSource = self;

    }

    return _RightTable;

}

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

    if (tableView.tag == 100) {

        return Proarr.count;

    }else

    {

        return 1;

    }

}

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

{

    if (tableView.tag == 100)

    {

        NSArray *arr = cityAtt[section];

        return arr.count;

    }

    else

    {

        return Proarr.count;

    }

}

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

{

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];

        if (!cell)

        {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"];

        }

    if (tableView.tag == 100)

    {

        cell.textLabel.text = cityAtt[indexPath.section][indexPath.row];

    }else

    {

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

    }

    return cell;

 

}

// 点击一个 另一个滚动到响应位置

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

{

    if (tableView.tag == 110)

    {

        NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];

        

        [self.LeftTable scrollToRowAtIndexPath:moveToIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

    }

}

// 滑动一个使另一个到具体位置

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    if (scrollView == self.LeftTable)

    {

        NSIndexPath *topHeaderViewIndexpath = [[self.LeftTable indexPathsForVisibleRows] firstObject];

        

        NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];

        

        [self.RightTable selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

    }

    

}

// 页眉

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

{

    if (tableView.tag == 100)

    {

        return Proarr[section];

    }else

    {

        return 0;

    }

}

// 页脚

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

{

    NSArray *arr = cityAtt[section];

    NSString *str = [NSString stringWithFormat:@"共有%lu个城市",(unsigned long)arr.count];

    if (tableView.tag == 100) {

        return str;

    }

    else

    {

        return 0;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值