最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果

 更新時(shí)間:2016年08月23日 16:02:14   作者:SunShineAku  
在我們?nèi)粘i_發(fā)IOS中,經(jīng)常見到兩個(gè)tableview的聯(lián)動(dòng),滑動(dòng)一側(cè)tableview,另一側(cè)tableview跟著滑動(dòng),其實(shí)實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,只是需要搞清楚他們之間的區(qū)別和聯(lián)系,下面一起來(lái)看看如何實(shí)現(xiàn)。

一、先來(lái)看看要實(shí)現(xiàn)的效果圖

二、小解析,可以先看看后面的!


三、實(shí)現(xiàn) tableView聯(lián)動(dòng) 主要分兩種狀況

     1、點(diǎn)擊 左側(cè) cell 讓右側(cè) tableView 滾到對(duì)應(yīng)位置

     2、滑動(dòng) 右側(cè) tableView 讓左側(cè) tableView 滾到對(duì)應(yīng)位置

1.先實(shí)現(xiàn)簡(jiǎn)單的:點(diǎn)擊 左側(cè) cell 讓右側(cè) tableView 滾到對(duì)應(yīng)位置

//MARK: - 點(diǎn)擊 cell 的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 // 判斷是否為 左側(cè) 的 tableView
 if (tableView == self.leftTableView) {

  // 計(jì)算出 右側(cè) tableView 將要 滾動(dòng)的 位置
  NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];

  // 將右側(cè) tableView 移動(dòng)到指定位置
  [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

  // 取消選中效果
  [self.rightTableView deselectRowAtIndexPath:moveToIndexPath animated:YES];
 }
}

左側(cè) 按鈕點(diǎn)擊的聯(lián)動(dòng) 搞定!

2.滑動(dòng) 右側(cè) tableView 讓左側(cè) tableView 滾到對(duì)應(yīng)位置

[self.rightTableView indexPathsForVisibleRows] 返回 所有顯示在界面的 cell 的 indexPath

//MARK: - 一個(gè)方法就能搞定 右邊滑動(dòng)時(shí)跟左邊的聯(lián)動(dòng)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

 // 如果是 左側(cè)的 tableView 直接return
 if (scrollView == self.leftTableView) return;

 // 取出顯示在 視圖 且最靠上 的 cell 的 indexPath
 NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject];

 // 左側(cè) talbelView 移動(dòng)到的位置 indexPath
 NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];

 // 移動(dòng) 左側(cè) tableView 到 指定 indexPath 居中顯示
 [self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

}

第二步 右側(cè) 滑動(dòng) 跟左側(cè) 的聯(lián)動(dòng) 搞定! 對(duì)的 就是這么簡(jiǎn)單!!!

四、警告

看到別人通過這兩個(gè)方法判斷!!! 勿用!!!

會(huì)導(dǎo)致 tableView 的聯(lián)動(dòng) 不準(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)動(dòng)
//
// 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è)方法就能搞定 右邊滑動(dòng)時(shí)跟左邊的聯(lián)動(dòng)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

 // 如果是 左側(cè)的 tableView 直接return
 if (scrollView == self.leftTableView) return;

 // 取出顯示在 視圖 且最靠上 的 cell 的 indexPath
 NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject];

 // 左側(cè) talbelView 移動(dòng)的 indexPath
 NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];

 // 移動(dòng) 左側(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 移動(dòng)到指定位置
  [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)動(dòng)效果的內(nèi)容到這就結(jié)束了,這種的效果在我們平常的時(shí)候還是挺常見的,感興趣的朋友們可以自己動(dòng)手操作起來(lái),希望對(duì)大家的學(xué)習(xí)工作能有所幫助。

相關(guān)文章

  • iOS簡(jiǎn)單畫板開發(fā)案例分享

    iOS簡(jiǎn)單畫板開發(fā)案例分享

    這篇文章主要為大家分享了iOS實(shí)現(xiàn)簡(jiǎn)單畫板開發(fā)案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • iOS自定義相機(jī)實(shí)現(xiàn)拍照、錄制視頻

    iOS自定義相機(jī)實(shí)現(xiàn)拍照、錄制視頻

    這篇文章主要為大家詳細(xì)介紹了iOS自定義相機(jī)實(shí)現(xiàn)拍照、錄制視頻,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iOS開發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤自定義按鍵

    iOS開發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤自定義按鍵

    這篇文章主要介紹了iOS開發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤自定義按鍵 的相關(guān)資料,需要的朋友可以參考下
    2016-08-08
  • iOS中NSNumberFormatter的介紹與用法

    iOS中NSNumberFormatter的介紹與用法

    NSNumberFormatter 應(yīng)該可以滿足你對(duì)數(shù)據(jù)形式的一般需求,值得了解一下,下面這篇文章主要給大家介紹了關(guān)于iOS中NSNumberFormatter的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • 詳解使用ISO鏡像搭建私有yum源

    詳解使用ISO鏡像搭建私有yum源

    這篇文章主要介紹了詳解使用ISO鏡像搭建私有yum源,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-06-06
  • iOS開發(fā)技巧之自定義相機(jī)

    iOS開發(fā)技巧之自定義相機(jī)

    這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)技巧之自定義相機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Xcode中iOS應(yīng)用開發(fā)的一般項(xiàng)目目錄結(jié)構(gòu)和流程簡(jiǎn)介

    Xcode中iOS應(yīng)用開發(fā)的一般項(xiàng)目目錄結(jié)構(gòu)和流程簡(jiǎn)介

    這篇文章主要介紹了Xcode中iOS應(yīng)用開發(fā)的一般項(xiàng)目目錄結(jié)構(gòu)和流程簡(jiǎn)介,包括項(xiàng)目所需的一些平臺(tái)路徑如模擬器路徑等的介紹,需要的朋友可以參考下
    2016-02-02
  • 詳解iOS 實(shí)現(xiàn)一對(duì)多代理方案

    詳解iOS 實(shí)現(xiàn)一對(duì)多代理方案

    本文主要介紹了iOS 實(shí)現(xiàn)一對(duì)多代理方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • iOS開發(fā)之獲取LaunchImage啟動(dòng)圖的實(shí)例

    iOS開發(fā)之獲取LaunchImage啟動(dòng)圖的實(shí)例

    下面小編就為大家分享一篇iOS開發(fā)之獲取LaunchImage啟動(dòng)圖的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2017-12-12
  • IOS開發(fā)中NSURL的基本操作及用法詳解

    IOS開發(fā)中NSURL的基本操作及用法詳解

    NSURL其實(shí)就是我們?cè)跒g覽器上看到的網(wǎng)站地址,這不就是一個(gè)字符串么,為什么還要在寫一個(gè)NSURL呢,主要是因?yàn)榫W(wǎng)站地址的字符串都比較復(fù)雜,包括很多請(qǐng)求參數(shù),這樣在請(qǐng)求過程中需要解析出來(lái)每個(gè)部門,所以封裝一個(gè)NSURL,操作很方便
    2015-12-12

最新評(píng)論

武胜县| 锡林浩特市| 九台市| 承德县| 霍州市| 汉寿县| 武平县| 邓州市| 应用必备| 乌鲁木齐县| 神木县| 宽甸| 巴林右旗| 陵水| 洮南市| 建始县| 德惠市| 开远市| 岚皋县| 广南县| 广灵县| 阳原县| 义乌市| 宁明县| 微山县| 定兴县| 东光县| 招远市| 沽源县| 随州市| 米林县| 招远市| 黄石市| 库车县| 西丰县| 大名县| 磐安县| 南宫市| 五华县| 茶陵县| 黄梅县|