服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS实现左右两个TableView联动效果

IOS实现左右两个TableView联动效果

2021-01-24 15:22SunShineAku IOS

在我们日常开发IOS中,经常见到两个tableview的联动,滑动一侧tableview,另一侧tableview跟着滑动,其实实现起来比较简单,只是需要搞清楚他们之间的区别和联系,下面一起来看看如何实现。

一、先来看看要实现的效果图

IOS实现左右两个TableView联动效果

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

IOS实现左右两个TableView联动效果

三、实现 tableview联动 主要分两种状况

     1、点击 左侧 cell 让右侧 tableview 滚到对应位置

     2、滑动 右侧 tableview 让左侧 tableview 滚到对应位置

1.先实现简单的:点击 左侧 cell 让右侧 tableview 滚到对应位置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//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 的联动 不准确

?
1
2
3
4
5
6
7
8
9
10
11
#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 已经显示
// 这两个方法都不准确
}

五、以下是所有示例代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// 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联动效果的内容到这就结束了,这种的效果在我们平常的时候还是挺常见的,感兴趣的朋友们可以自己动手操作起来,希望对大家的学习工作能有所帮助。

原文链接:http://www.jianshu.com/p/b2589fff500a

延伸 · 阅读

精彩推荐
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28