ios實現兩個tableview聯動

兩個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];

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章