兩個UITableView 左右關(guān)聯(lián)

1.點(diǎn)擊左側(cè)cell,讓右邊tableview滾動到相應(yīng)位置
2.滑動右側(cè)tableview,讓左側(cè)tableview選中相應(yīng)cell

寫過好幾次了,稍微理解一下,其實(shí)簡單的

效果

Untitled.gif

創(chuàng)建兩個tableview

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

loadTableView

    UITableView *ltableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, HHBWIDTH * 0.3, HHBHEIGHT-64-40)];
    _leftTableView = ltableView;
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    _leftTableView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    _leftTableView.sectionHeaderHeight = 38;
    _leftTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _leftTableView.separatorStyle = UITableViewScrollPositionNone;
    [self.view addSubview:_leftTableView];

    
    UITableView *rtableView = [[UITableView alloc] initWithFrame:CGRectMake(HHBWIDTH * 0.3, 0, HHBWIDTH * 0.7, HHBHEIGHT-64-40)];
    _rightTableView = rtableView;
    _rightTableView.delegate = self;
    _rightTableView.dataSource = self;
    _rightTableView.backgroundColor = SELFBGColor;
    _rightTableView.sectionHeaderHeight = 38;
    _rightTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_rightTableView];

常規(guī)代碼片

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == _rightTableView) {
        return _rightdataSoure.count;
    }else{
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _leftTableView) {
        return _leftdataSoure.count;
    }else{
        return [[_rightdataSoure[section] objectForKey:@"title"] count];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _leftTableView) {
        return 43;
    }
    return 30+48*[HuPageConfig plus_MagnifyingPower];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _leftTableView) {
        
        static NSString *cellID = @"cellID";
        inHospitalLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (nil == cell) {
            cell = [[inHospitalLeftTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.title.text = @"心血管內(nèi)科";
        
        return cell;
    }else{
        static NSString *cellID = @"cellID";
        inHospitalRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (nil == cell) {
            cell = [[inHospitalRightTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
}

點(diǎn)擊左側(cè)Cell效果

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _leftTableView) {
        NSIndexPath *moveIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];

        //animated為YES會導(dǎo)致leftTableView選擇亂跳
        [_rightTableView selectRowAtIndexPath:moveIndexPath animated:NO scrollPosition:UITableViewScrollPositionTop];

        [_rightTableView deselectRowAtIndexPath:moveIndexPath animated:YES];
    }else {
        
    }
}

滑動右側(cè)tableview 效果

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView == self.leftTableView) {
        return;
    }

    NSIndexPath *topIndexPath = [[_rightTableView indexPathsForVisibleRows]firstObject];
    NSIndexPath *moveIndexPath = [NSIndexPath indexPathForRow:topIndexPath.section inSection:0];

    [_leftTableView selectRowAtIndexPath:moveIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}

小白總結(jié),歡迎打臉指正

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,093評論 3 38
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,259評論 4 61
  • 黃昏,去河邊散步 感覺一只鷹飛過我們的頭頂 它銳利的呼叫聲 淹沒了河水的喧囂 它云一樣的翅膀 覆蓋了我在塵世的憂傷...
    甘肅子溪閱讀 289評論 2 1
  • 2016年北京,西安,銀川,廣州,濟(jì)南到處跑,學(xué)習(xí)身心靈,就是想要自己過得很好! 學(xué)費(fèi)沒少交,車票一打打的。這不就...
    瀞好如琳閱讀 225評論 0 1