iOS tableView聯(lián)動

大致實(shí)現(xiàn)的效果圖:
效果圖.gif

方一:利用collectionView和tableView實(shí)現(xiàn)聯(lián)動.(以后更新)
方法二:利用兩個tablview實(shí)現(xiàn)聯(lián)動。
我的想法可能有點(diǎn)奇特啊,哈哈,上面是個小tableview,將tableView和tableViewCell進(jìn)行了了角度旋轉(zhuǎn),
廢話不多說上代碼。
創(chuàng)建兩個tableView

self.topTableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, 40, SCREEN_WIDTH) style:UITableViewStylePlain];
    [self.view addSubview:_topTableView];
    self.topTableView.delegate = self;
    self.topTableView.dataSource = self;
    self.topTableView.showsVerticalScrollIndicator = NO;
    [self.topTableView registerClass:[BBAllZhuanChangTableViewCell class] forCellReuseIdentifier:@"top"];
    self.topTableView.transform = CGAffineTransformMakeRotation((-M_PI_2));//將tableView進(jìn)行了90旋轉(zhuǎn)
    self.topTableView.x = 0;
    self.topTableView.y = 0;
    self.topTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   // self.topTableView.frame = CGRectMake(0,100,SCREEN_WIDTH, 40);
    //默認(rèn)選中第0個cell
    [_topTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
    
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_HEIGHT - 40) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];

我們還需要將上部分cell進(jìn)行角度旋轉(zhuǎn) ,tableView代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _topTableView) {
        BBAllZhuanChangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"top" forIndexPath:indexPath];
        cell.nameLabel.text = self.types[indexPath.row];
        cell.transform = CGAffineTransformMakeRotation(M_PI_2);
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    return cell;
}

先實(shí)現(xiàn)滑動下面大tableView聯(lián)動

// 當(dāng)前的 tableView 是 tableView,tableView 滾動的方向向上,tableView 是用戶拖拽而產(chǎn)生滾動的((_isScrollDown主要判斷 tableView 用戶拖拽而滾動的,還是點(diǎn)擊 topTableView 而滾動的)
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ( (tableView == self.tableView) && !_isScrollDown && (self.tableView.dragging || self.tableView.isDragging)) {
          NSLog(@"向上-----%ld",section);
       
        [self selectRow:section];
    }



}
 // 當(dāng)前的 tableView 是 tableView,tableView 滾動的方向向上,tableView 是用戶拖拽而產(chǎn)生滾動的((_isScrollDown主要判斷 tableView 用戶拖拽而滾動的,還是點(diǎn)擊 topTableView 而滾動的)
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(nonnull UIView *)view forSection:(NSInteger)section {
    if ((tableView == self.tableView) && _isScrollDown && (self.tableView.dragging || self.tableView.isDragging)) {
        NSLog(@"向下=====%ld",section + 1);
        [self selectRow:section];
        
    }

}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
    // 標(biāo)記一下 self.tableView 的滾動方向,是向上還是向下
    if (self.tableView == scrollView) {
        static CGFloat lastOffsetY = 0;
        
        if (self.tableView == scrollView) {
            _isScrollDown = lastOffsetY < scrollView.contentOffset.y;
            lastOffsetY = scrollView.contentOffset.y;
        }
}
//拖動下邊tableView處理
- (void)selectRow:(NSInteger)index {
    if (index <= self.types.count - 1) {
        [self.topTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
   }
}

點(diǎn)擊上面tableView聯(lián)動

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _topTableView) {
        self.selectedIndex = indexPath.row;
       
        [self scrollToTop:_selectedIndex animate:YES];
        [_topTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
       //[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:_selectedIndex] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    
}
//點(diǎn)擊上面tableview處理
- (void)scrollToTop:(NSInteger)section animate:(BOOL)isanimated {
  
    CGRect headerRect = [self.tableView rectForSection:section];
    //NSLog(@"%f--%f",headerRect.origin.y,self.tableView.contentInset.top);
    CGPoint topOfHeadr = CGPointMake(0, headerRect.origin.y);
    [self.tableView setContentOffset:topOfHeadr animated:YES];
}

大概和核心代碼。好的還有自定cell的處理,一起都粘貼出來吧

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.highlighted = selected;
    self.nameLabel.highlighted = selected;
    self.redView.hidden = !selected;
    // Configure the view for the selected state
}

ok了,代碼沒有整理,寫在項目了,有需要的我可以給你個demo。好久沒寫文章。隨便寫寫吧。希望對大家能有幫助。
菜雞一個,努力學(xué)習(xí)!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容