国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS實現左右兩個TableView聯動效果

2020-07-26 03:15:55
字體:
來源:轉載
供稿:網友

一、先來看看要實現的效果圖

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


三、實現 tableView聯動 主要分兩種狀況

     1、點擊 左側 cell 讓右側 tableView 滾到對應位置

     2、滑動 右側 tableView 讓左側 tableView 滾到對應位置

1.先實現簡單的:點擊 左側 cell 讓右側 tableView 滾到對應位置

//MARK: - 點擊 cell 的代理方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 判斷是否為 左側 的 tableView if (tableView == self.leftTableView) {  // 計算出 右側 tableView 將要 滾動的 位置  NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];  // 將右側 tableView 移動到指定位置  [self.rightTableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];  // 取消選中效果  [self.rightTableView deselectRowAtIndexPath:moveToIndexPath animated:YES]; }}

左側 按鈕點擊的聯動 搞定!

2.滑動 右側 tableView 讓左側 tableView 滾到對應位置

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

//MARK: - 一個方法就能搞定 右邊滑動時跟左邊的聯動- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 如果是 左側的 tableView 直接return if (scrollView == self.leftTableView) return; // 取出顯示在 視圖 且最靠上 的 cell 的 indexPath NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject]; // 左側 talbelView 移動到的位置 indexPath NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0]; // 移動 左側 tableView 到 指定 indexPath 居中顯示 [self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];}

第二步 右側 滑動 跟左側 的聯動 搞定! 對的 就是這么簡單!!!

四、警告

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

會導致 tableView 的聯動 不準確

#pragma mark - UITableViewDelegate 代理方法 -- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {//// headerView 將要顯示// 這兩個方法都不準確}- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {//// headerView 已經顯示// 這兩個方法都不準確}

五、以下是所有示例代碼

//// ViewController.m// 左右雙tableView聯動//// 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 數據源代理方法 -- (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 {//// 這兩個方法都不準確//}////- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {//// 這兩個方法都不準確//}//MARK: - 一個方法就能搞定 右邊滑動時跟左邊的聯動- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 如果是 左側的 tableView 直接return if (scrollView == self.leftTableView) return; // 取出顯示在 視圖 且最靠上 的 cell 的 indexPath NSIndexPath *topHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject]; // 左側 talbelView 移動的 indexPath NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0]; // 移動 左側 tableView 到 指定 indexPath 居中顯示 [self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];}//MARK: - 點擊 cell 的代理方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 選中 左側 的 tableView if (tableView == self.leftTableView) {  NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];  // 將右側 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

六、總結

IOS實現左右兩個TableView聯動效果的內容到這就結束了,這種的效果在我們平常的時候還是挺常見的,感興趣的朋友們可以自己動手操作起來,希望對大家的學習工作能有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀远县| 五华县| 本溪| 威远县| 体育| 灌阳县| 珠海市| 东辽县| 永济市| 安龙县| 玉田县| 峨眉山市| 娄烦县| 北安市| 平昌县| 亚东县| 墨玉县| 永济市| 彩票| 门头沟区| 定结县| 龙海市| 册亨县| 抚宁县| 临颍县| 凌海市| 盐津县| 军事| 澳门| 买车| 阿荣旗| 峨山| 井陉县| 土默特右旗| 三门县| 临夏县| 余干县| 景谷| 杭州市| 遂溪县| 天祝|