tableView列表聯(lián)動

最近項目中需要做一個兩個tableVIew聯(lián)動的效果,有點類似于餓了嗎外賣點餐的那種效果,雖然實現(xiàn)起來難度不是太大,但是還是記錄下來,方便有需要的開發(fā)者少走一些彎路.個人認(rèn)為做什么效果主要還是一個思路問題,思路對了,做起來也就得心應(yīng)手了.廢話不多說,先附上效果圖.

示例.gif

具體實現(xiàn)步驟

  • 1 . 首先確定好思路,底層是用viewController, 上面的地址和派送次數(shù)的視圖是用兩個View做的,下面兩個tableView ,左邊是leftTableView , 右邊是rightTableView.這樣做的目的是為了tableView滑動到頂部的時候整個視圖有個上移的動畫效果,這樣做更加方便一點.

    圖1.png

  • 2.接下來創(chuàng)建2個viewleftTableView,rightTableView遵循代理,加載數(shù)據(jù)源.進(jìn)入頁面默認(rèn)選中leftTableView第一個單元格,這些步驟忽略.

默認(rèn)選中第一個單元格
 [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
  • 3.點擊左側(cè)tableView的時候,讓右側(cè)的tableView滾動到指定的分區(qū).實現(xiàn)tableView的代理方法.取出當(dāng)前的分區(qū)的indexpatg.row就是rightTbaleView的分區(qū)section.
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:  (NSIndexPath *)indexPath {
    if (_leftTableView == tableView) {
        AYLeftPickTableViewCell *leftCell = [tableView cellForRowAtIndexPath:indexPath];
        leftCell.contentView.backgroundColor = KLightYellowColor;
        
        NSArray *array = [tableView visibleCells];
        for (AYLeftPickTableViewCell *leftCell in array) {
            // 不打?qū)?            leftCell.titleLabel.textColor = [UIColor blackColor];
        }
        // 打?qū)?        leftCell.titleLabel.textColor = kOrangeTextColor;
        [_rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
  • 4.滾動rightTableView的時候讓左側(cè)tableView滾動到指定的行,也就是rightTableView的分區(qū)section,和leftTableViewindexPath.row是相對應(yīng)的.實現(xiàn)scrollView的代理方法.
   - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 滑動視圖上移動畫
    [self viewUpAnimationWihtScrollView:scrollView];
    if (scrollView == self.rightTableView) {
        //取出當(dāng)前顯示的最頂部的cell的indexpath
        //當(dāng)前tableview頁面可見的分區(qū)屬性 indexPathsForVisibleRows
        // 取出顯示在 視圖 且最靠上 的 cell 的 indexPath
        // 判斷tableView是否滑動到最底部
        CGFloat height = scrollView.frame.size.height;
        CGFloat contentOffsetY = scrollView.contentOffset.y;
        CGFloat bottomOffset = scrollView.contentSize.height - contentOffsetY;
        if (bottomOffset <= height) {
            NSIndexPath *bottomIndexPath = [[self.rightTableView indexPathsForVisibleRows] lastObject];
            NSIndexPath *moveIndexPath = [NSIndexPath indexPathForRow:bottomIndexPath.section inSection:0];
            [self.leftTableView selectRowAtIndexPath:moveIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
        } else {
            NSIndexPath *topIndexPath = [[self.rightTableView indexPathsForVisibleRows]firstObject];
            NSIndexPath *moveIndexPath = [NSIndexPath indexPathForRow:topIndexPath.section inSection:0];
            [self.leftTableView selectRowAtIndexPath:moveIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
        }
    }else{
        return;
    }
}

  • 5.當(dāng)滑動tableView的時候添加動畫,tableView向上滑動的時候讓View向上偏移.這一步在scrollViewDidScroll:(UIScrollView *)scrollView中調(diào)用.
- (void)viewUpAnimationWihtScrollView:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > 0) {
        [self.addressView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(-Address_Height+64);
            make.height.mas_equalTo(Address_Height);
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            [self.view layoutIfNeeded];
            self.rightTableView.frame = CGRectMake(LeftTable_Width, SendView_Height+64, kScreenWidth - LeftTable_Width, kScreenHeight - 64 - 50 - SendView_Height);
            self.leftTableView.frame = CGRectMake(0,  SendView_Height+64, LeftTable_Width, kScreenHeight - 64 - 50 - SendView_Height);
        } completion:^(BOOL finished) {
            nil;
        }];
    } else {
        [self.addressView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(64);
            make.height.mas_equalTo(Address_Height);
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            [self.view layoutIfNeeded];
            self.rightTableView.frame = CGRectMake(LeftTable_Width, Address_Height + SendView_Height+64, kScreenWidth - LeftTable_Width, kScreenHeight - 64 - 50 - Address_Height - SendView_Height);
            self.leftTableView.frame = CGRectMake(0, Address_Height + SendView_Height+64, LeftTable_Width, kScreenHeight - 64 - 50 - Address_Height - SendView_Height);
        } completion:^(BOOL finished) {
            nil;
        }];
    }
}

這樣就實現(xiàn)了兩個tableView聯(lián)動的效果,其中有2個方法比較關(guān)鍵:

  • -1 .點擊leftTableView的時候讓rightTableView滾動到指定分區(qū).
    [_rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  • -2 .rightTableView滾動的時候,獲取當(dāng)前頁面可見的分區(qū)行數(shù),
    NSIndexPath *topIndexPath = [[self.rightTableView indexPathsForVisibleRows]firstObject];

優(yōu)化處理

--- 根據(jù)賣香蕉的大叔的建議,對tableView單選處理進(jìn)行優(yōu)化.以前我寫的那種方式是用NSArray *array = [tableView visibleCells];方法遍歷出所有的單元格,然后重新改變title的顏色和cell的背景顏色,然后給當(dāng)前點擊的這個單元格重新改變新的顏色,這樣的操作不利于tableView的優(yōu)化,如果cell足夠多的時候,這樣做會造成界面卡頓.

  • 1.把最后點擊的最后點擊的cellindexPath定義成屬性.
@property(nonatomic, strong) NSIndexPath *lastPath;  // 單選
  • 2.在點擊cell的時候記錄下最新的indexPath,并且把最新點擊的cell上的title和背景顏色進(jìn)行修改,把以前的cell再更改回原始狀態(tài).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_leftTableView == tableView) {
        NSInteger newRow = [indexPath row];
        NSInteger oldRow = (self .lastPath !=nil)?[self .lastPath row]:-1;
        if (newRow != oldRow) {
            AYLeftPickTableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
            newCell.contentView.backgroundColor = KLightYellowColor;
            newCell.titleLabel.textColor = kOrangeTextColor;
            AYLeftPickTableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastPath];
            oldCell.contentView.backgroundColor = [UIColor clearColor];
            oldCell.titleLabel.textColor = [UIColor blackColor];
        }
        self.lastPath = indexPath;
        [_rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
  • 3.為了防止單元格重用出現(xiàn)問題,需要在cellForRowAtIndexPath中判斷當(dāng)前cell的選中狀態(tài).
    AYLeftPickTableViewCell *leftCell = [tableView dequeueReusableCellWithIdentifier:@"AYLeftPickTableViewCell" forIndexPath:indexPath];
        AYCollectFoodModel *model = self.dataArray[indexPath.row];
        leftCell.titleLabel.text = model.vegname;
        leftCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSInteger row = [indexPath row];
        NSInteger oldRow = [_lastPath row];
        if (row == oldRow && _lastPath!=nil) {
            // 被選中狀態(tài)
            leftCell.contentView.backgroundColor = KLightYellowColor;
            leftCell.titleLabel.textColor = kOrangeTextColor;
        }else{
            leftCell.contentView.backgroundColor = [UIColor clearColor];
            leftCell.titleLabel.textColor = [UIColor blackColor];
        }

這種處理適用于自定義單元格的單選處理,在這里也正好記錄下來,方便給需要用到此功能的開發(fā)者多一些思路,再次感謝賣香蕉的大叔給出的意見.

--- 感謝TryToFlyHigher提出的bug,這個bug是由于點擊左側(cè)leftTableView讓右側(cè)rightTableView滾動的時候,導(dǎo)致左側(cè)的leftTableView又重新選中了一次,就像選中框閃了一下的感覺.目前這個bug已經(jīng)解決,主要思路是定義一個BOOL值表示是否重復(fù)滾動,在左側(cè)leftTableView選中的時候把BOOL值置為YES,在scrollView即將拖動的時候置為NO,在- (void)scrollViewDidScroll:(UIScrollView *)scrollView方法中加上判斷,是否重復(fù)滾動.

  • 1.定義BOOL值表示是否重復(fù)滾動,并給默認(rèn)值為NO
 @property (nonatomic, assign) BOOL isRepeatRolling;  // 是否重復(fù)滾動
 self.isRepeatRolling = NO; // 默認(rèn)NO
    1. tableView滾動的時候把值置為YES.表示重復(fù)選中了,并在scrollViewDidScroll:(UIScrollView *)scrollView添加判斷是否重復(fù)滾動
 if (self.isRepeatRolling == NO) { // 防止重復(fù)滾動
    [self.leftTableView selectRowAtIndexPath:moveIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  }
  • 3.在scrollView開始拖動的時候更改BOOL值為NO
// scrollView 開始拖動
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.isRepeatRolling = NO;
}

趁著這次打開項目解決掉這個bug,順便適配了一下iOS11.
gitHub已經(jīng)更新,再次感謝TryToFlyHigher提出的bug.

結(jié)尾

在項目中使用了MJExtension字典轉(zhuǎn)模型,和masonry屏幕適配.
至此就大概實現(xiàn)了列表聯(lián)動的效果,可以實現(xiàn)的方法有很多,在這里只是提出一個思路,讓開發(fā)者能夠少走一些彎路.

另附本文gitHub地址 ,喜歡的給個星星哦.非常感謝..

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谅畅,一起剝皮案震驚了整個濱河市欲间,隨后出現(xiàn)的幾起案子麻惶,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件踪旷,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鲤妥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拱雏,“玉大人棉安,你說我怎么就攤上這事≈郑” “怎么了贡耽?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鹊汛。 經(jīng)常有香客問我蒲赂,道長,這世上最難降的妖魔是什么刁憋? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任滥嘴,我火速辦了婚禮,結(jié)果婚禮上至耻,老公的妹妹穿的比我還像新娘若皱。我一直安慰自己,他們只是感情好尘颓,可當(dāng)我...
    茶點故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布是尖。 她就那樣靜靜地躺著,像睡著了一般泥耀。 火紅的嫁衣襯著肌膚如雪饺汹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天痰催,我揣著相機(jī)與錄音兜辞,去河邊找鬼迎瞧。 笑死,一個胖子當(dāng)著我的面吹牛逸吵,可吹牛的內(nèi)容都是我干的凶硅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼扫皱,長吁一口氣:“原來是場噩夢啊……” “哼足绅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起韩脑,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤氢妈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后段多,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體首量,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年进苍,在試婚紗的時候發(fā)現(xiàn)自己被綠了加缘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,567評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡觉啊,死狀恐怖拣宏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情杠人,我是刑警寧澤勋乾,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站搜吧,受9級特大地震影響市俊,放射性物質(zhì)發(fā)生泄漏杨凑。R本人自食惡果不足惜滤奈,卻給世界環(huán)境...
    茶點故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望撩满。 院中可真熱鬧蜒程,春花似錦、人聲如沸伺帘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽伪嫁。三九已至领炫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間张咳,已是汗流浹背帝洪。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工似舵, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人葱峡。 一個月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓砚哗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親砰奕。 傳聞我的和親對象是個殘疾皇子蛛芥,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,585評論 2 359

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