IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動效果
一、先來看看要實(shí)現(xiàn)的效果圖
二、小解析,可以先看看后面的!
三、實(shí)現(xiàn) tableView聯(lián)動 主要分兩種狀況
1、點(diǎn)擊 左側(cè) cell 讓右側(cè) tableView 滾到對應(yīng)位置
2、滑動 右側(cè) tableView 讓左側(cè) tableView 滾到對應(yīng)位置
1.先實(shí)現(xiàn)簡單的:點(diǎn)擊 左側(cè) cell 讓右側(cè) tableView 滾到對應(yīng)位置
//MARK: - 點(diǎn)擊 cell 的代理方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 判斷是否為 左側(cè) 的 tableView if (tableView == self.leftTableView) { // 計(jì)算出 右側(cè) tableView 將要 滾動的 位置 NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row]; // 將右側(cè) tableView 移動到指定位置 [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; // 取消選中效果 [self.rightTableView deselectRowAtIndexPath:moveToIndexPath animated:YES]; } }
左側(cè) 按鈕點(diǎn)擊的聯(lián)動 搞定!
2.滑動 右側(cè) tableView 讓左側(cè) tableView 滾到對應(yīng)位置
[self.rightTableView indexPathsForVisibleRows]
返回 所有顯示在界面的 cell 的 indexPath
//MARK: - 一個(gè)方法就能搞定 右邊滑動時(shí)跟左邊的聯(lián)動 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 如果是 左側(cè)的 tableView 直接return if (scrollView == self.leftTableView) return; // 取出顯示在 視圖 且最靠上 的 cell 的 indexPath NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject]; // 左側(cè) talbelView 移動到的位置 indexPath NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0]; // 移動 左側(cè) tableView 到 指定 indexPath 居中顯示 [self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; }
第二步 右側(cè) 滑動 跟左側(cè) 的聯(lián)動 搞定! 對的 就是這么簡單!!!
四、警告
看到別人通過這兩個(gè)方法判斷!!! 勿用!!!
會導(dǎo)致 tableView 的聯(lián)動 不準(zhǔn)確
#pragma mark - UITableViewDelegate 代理方法 - - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { // // headerView 將要顯示 // 這兩個(gè)方法都不準(zhǔn)確 } - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section { // // headerView 已經(jīng)顯示 // 這兩個(gè)方法都不準(zhǔn)確 }
五、以下是所有示例代碼
// // ViewController.m // 左右雙tableView聯(lián)動 // // Created by 阿酷 on 16/8/20. // Copyright © 2016年 AkuApp. All rights reserved. // #import "ViewController.h" #define leftTableWidth [UIScreen mainScreen].bounds.size.width * 0.3 #define rightTableWidth [UIScreen mainScreen].bounds.size.width * 0.7 #define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height #define leftCellIdentifier @"leftCellIdentifier" #define rightCellIdentifier @"rightCellIdentifier" @interface ViewController () <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, weak) UITableView *leftTableView; @property (nonatomic, weak) UITableView *rightTableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.leftTableView]; [self.view addSubview:self.rightTableView]; } #pragma mark - tableView 數(shù)據(jù)源代理方法 - - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.leftTableView) return 40; return 8; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == self.leftTableView) return 1; return 40; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; // 左邊的 view if (tableView == self.leftTableView) { cell = [tableView dequeueReusableCellWithIdentifier:leftCellIdentifier forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row]; // 右邊的 view } else { cell = [tableView dequeueReusableCellWithIdentifier:rightCellIdentifier forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"第%ld組-第%ld行", indexPath.section, indexPath.row]; } return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (tableView == self.rightTableView) return [NSString stringWithFormat:@"第 %ld 組", section]; return nil; } #pragma mark - UITableViewDelegate 代理方法 - //- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section { // // 這兩個(gè)方法都不準(zhǔn)確 //} // //- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { // // 這兩個(gè)方法都不準(zhǔn)確 //} //MARK: - 一個(gè)方法就能搞定 右邊滑動時(shí)跟左邊的聯(lián)動 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 如果是 左側(cè)的 tableView 直接return if (scrollView == self.leftTableView) return; // 取出顯示在 視圖 且最靠上 的 cell 的 indexPath NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject]; // 左側(cè) talbelView 移動的 indexPath NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0]; // 移動 左側(cè) tableView 到 指定 indexPath 居中顯示 [self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; } //MARK: - 點(diǎn)擊 cell 的代理方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 選中 左側(cè) 的 tableView if (tableView == self.leftTableView) { NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row]; // 將右側(cè) tableView 移動到指定位置 [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; // 取消選中效果 [self.rightTableView deselectRowAtIndexPath:moveToIndexPath animated:YES]; } } #pragma mark - 懶加載 tableView - // MARK: - 左邊的 tableView - (UITableView *)leftTableView { if (!_leftTableView) { UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, leftTableWidth, ScreenHeight)]; [self.view addSubview:tableView]; _leftTableView = tableView; tableView.dataSource = self; tableView.delegate = self; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:leftCellIdentifier]; tableView.backgroundColor = [UIColor redColor]; tableView.tableFooterView = [[UIView alloc] init]; } return _leftTableView; } // MARK: - 右邊的 tableView - (UITableView *)rightTableView { if (!_rightTableView) { UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(leftTableWidth, 0, rightTableWidth, ScreenHeight)]; [self.view addSubview:tableView]; _rightTableView = tableView; tableView.dataSource = self; tableView.delegate = self; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:rightCellIdentifier]; tableView.backgroundColor = [UIColor cyanColor]; tableView.tableFooterView = [[UIView alloc] init]; } return _rightTableView; } @end
六、總結(jié)
IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動效果的內(nèi)容到這就結(jié)束了,這種的效果在我們平常的時(shí)候還是挺常見的,感興趣的朋友們可以自己動手操作起來,希望對大家的學(xué)習(xí)工作能有所幫助。
相關(guān)文章
詳解iOS 關(guān)于字體根據(jù)不同屏幕尺寸等比適配的問題
這篇文章主要介紹了詳解iOS 關(guān)于字體根據(jù)不同屏幕尺寸等比適配的問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06iOS在頁面銷毀時(shí)如何優(yōu)雅的cancel網(wǎng)絡(luò)請求詳解
這篇文章主要給大家介紹了關(guān)于iOS在頁面銷毀時(shí)如何優(yōu)雅的cancel網(wǎng)絡(luò)請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05Ios蘋果app應(yīng)用程序開發(fā)者如何獲取IPA簽名證書詳解
這篇文章主要為大家介紹了Ios蘋果app應(yīng)用程序開發(fā)者如何獲取IPA簽名證書詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)
這篇文章主要介紹了IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03IOS 開發(fā)之網(wǎng)絡(luò)圖片輪播圖的實(shí)現(xiàn)
這篇文章主要介紹了IOS 開發(fā)之網(wǎng)絡(luò)圖片輪播圖的實(shí)現(xiàn)的相關(guān)資料,希望通過此文大家能夠掌握輪播圖的實(shí)現(xiàn),需要的朋友可以參考下2017-09-09Flutter?Module添加到iOS項(xiàng)目示例詳解
這篇文章主要為大家介紹了Flutter?Module添加到iOS項(xiàng)目示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08IOS開發(fā)Objective-C?Runtime使用示例詳解
這篇文章主要為大家介紹了IOS開發(fā)Objective-C?Runtime使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02iOS正則表達(dá)式驗(yàn)證手機(jī)號、郵箱、身份證號等
這篇文章主要介紹了iOS正則表達(dá)式驗(yàn)證手機(jī)號、郵箱、身份證號等信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12詳解iOS App設(shè)計(jì)模式開發(fā)中對于享元模式的運(yùn)用
這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中對于享元模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04