美文网首页
两个UITableView 左右关联

两个UITableView 左右关联

作者: Chase_Eleven | 来源:发表于2017-06-21 09:40 被阅读0次

    1.点击左侧cell,让右边tableview滚动到相应位置
    2.滑动右侧tableview,让左侧tableview选中相应cell

    写过好几次了,稍微理解一下,其实简单的

    效果

    Untitled.gif

    创建两个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];
    

    常规代码片

    - (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 = @"心血管内科";
            
            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;
        }
        
    }
    
    

    点击左侧Cell效果

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView == _leftTableView) {
            NSIndexPath *moveIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
    
            //animated为YES会导致leftTableView选择乱跳
            [_rightTableView selectRowAtIndexPath:moveIndexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
    
            [_rightTableView deselectRowAtIndexPath:moveIndexPath animated:YES];
        }else {
            
        }
    }
    

    滑动右侧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];
    }
    

    小白总结,欢迎打脸指正

    相关文章

      网友评论

          本文标题:两个UITableView 左右关联

          本文链接:https://www.haomeiwen.com/subject/iadzqxtx.html