实现外卖选餐时两级 tableView 联动效果
2016-12-15 09:56
363 查看
效果图:
联动效果
下面说下具体实现步骤:
首先分解一下,实现这个需求主要是两点,一是点击左边tableView,同时滚动右边tableView到具体的位置。二是拖动右边tableView选中左边tableView对应的某一行。要实现这个需求有一点很重要:左边的tableView每一行对应的是右边tableView的每个分区.
实现点击左边tableView同时滚动右边tableView,很简单,只需要实现tableView的代理方法- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath;然后在代理方法里边拿到右边的tableView,实现让其滚动到第indexPath.row分区,第0行即可,代码如下:
我们这里不处理右边tableView的点击事件,所以if (tableView == self.rightTableView) return;
接下来我们实现 拖动右边tableView选中左边tableView对应的某一行,我们要动态选中左边的tableView,就需要拿到现在滚动到了那个分区,UITableView有两个代理方法,- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section // 一个头标题即将显示的时候掉用和- (void)tableView:(UITableView )tableView didEndDisplayingHeaderView:(UIView )view forSection:(NSInteger)section // 一个头标题即将消失的时候掉用
利用这两个方法就可以拿到当前所在分区实现这个功能了。
还有个更简单的方法,叫做indexPathsForVisibleRows,官方文档解释是:
简单意思就是,它返回一个装着目前屏幕上可见的cell的indexPath集合。
稍微解释一下,首先监听scrollView的拖动,本demo不处理左边tableView的滚动,所以if ((UITableView *)scrollView == self.leftTableView) return;
self.rightTableView.indexPathsForVisibleRows.firstObject.section这句是拿到当前屏幕上可见cell的第一行cell所在的分区,然后让左边的tableView选中第0分区(它只有一个分区)的这一行就OK了。
补充下:点击左边tableView的时候会有阴影效果,其实是这样的,点击左边的tableView,右边的tableView是从当前位置动画滚动到相应位置的,既然有滚动,就会调- (void)scrollViewDidScroll:(UIScrollView *)scrollView这个代理方法,说白了就是拖动了右边tableView,拖动右边的过程中会陆续选中左边。那我想大家就明白了。
如果不想要这个效果,有两个办法,一个是直接吧- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath中的动画滚动的属性animated值改成NO
这样做右边的tableView就是无动画滚动了,也就不会再调scrollViewDidScroll:方法。但是如果还想右边tableViewyou滚动效果,另一种解决方法是:把- (void)scrollViewDidScroll:(UIScrollView )scrollView方法换成- (void)scrollViewDidEndDecelerating:(UIScrollView )scrollView这个代理方法方法就行了。
这个方法也有一个bug:
这个方法调用与否在于你的手指是否在动画停止之前离开了屏幕,如果在动画结束之前手指离开屏幕,此方法调用没什么问题。but,如果动画已经停止,再把手指拿开,这个方法是不会调的。
解决这个bug的关键在于,让手指离开的时候手动调一次这个代理方法,那怎么才能知道手指什么时候离开呢?scrollView给我们了另一个代理方法:- (void)scrollViewWillEndDragging:(UIScrollView )scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint )targetContentOffset这个方法在结束拖拽的时候调,正好解决了我们的问题:
联动效果
下面说下具体实现步骤:
首先分解一下,实现这个需求主要是两点,一是点击左边tableView,同时滚动右边tableView到具体的位置。二是拖动右边tableView选中左边tableView对应的某一行。要实现这个需求有一点很重要:左边的tableView每一行对应的是右边tableView的每个分区.
实现点击左边tableView同时滚动右边tableView,很简单,只需要实现tableView的代理方法- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath;然后在代理方法里边拿到右边的tableView,实现让其滚动到第indexPath.row分区,第0行即可,代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 如果点击的是右边的tableView,不做任何处理 if (tableView == self.rightTableView) return; // 点击左边的tableView,设置选中右边的tableView某一行。左边的tableView的每一行对应右边tableView的每个分区 [self.rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop]; }
我们这里不处理右边tableView的点击事件,所以if (tableView == self.rightTableView) return;
接下来我们实现 拖动右边tableView选中左边tableView对应的某一行,我们要动态选中左边的tableView,就需要拿到现在滚动到了那个分区,UITableView有两个代理方法,- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section // 一个头标题即将显示的时候掉用和- (void)tableView:(UITableView )tableView didEndDisplayingHeaderView:(UIView )view forSection:(NSInteger)section // 一个头标题即将消失的时候掉用
利用这两个方法就可以拿到当前所在分区实现这个功能了。
还有个更简单的方法,叫做indexPathsForVisibleRows,官方文档解释是:
The value of this property is an array of NSIndexPath objects each representing a row index and section index that together identify a visible row in the table view. If no rows are visible, the value is nil.
简单意思就是,它返回一个装着目前屏幕上可见的cell的indexPath集合。
#pragma mark - UIScrollViewDelegate -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ // 监听tableView滑动 // 如果现在滑动的是左边的tableView,不做任何处理 if ((UITableView *)scrollView == self.leftTableView) return; // 滚动右边tableView,设置选中左边的tableView某一行。indexPathsForVisibleRows属性返回屏幕上可见的cell的indexPath数组,利用这个属性就可以找到目前所在的分区 [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.rightTableView.indexPathsForVisibleRows.firstObject.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle]; }
稍微解释一下,首先监听scrollView的拖动,本demo不处理左边tableView的滚动,所以if ((UITableView *)scrollView == self.leftTableView) return;
self.rightTableView.indexPathsForVisibleRows.firstObject.section这句是拿到当前屏幕上可见cell的第一行cell所在的分区,然后让左边的tableView选中第0分区(它只有一个分区)的这一行就OK了。
补充下:点击左边tableView的时候会有阴影效果,其实是这样的,点击左边的tableView,右边的tableView是从当前位置动画滚动到相应位置的,既然有滚动,就会调- (void)scrollViewDidScroll:(UIScrollView *)scrollView这个代理方法,说白了就是拖动了右边tableView,拖动右边的过程中会陆续选中左边。那我想大家就明白了。
如果不想要这个效果,有两个办法,一个是直接吧- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath中的动画滚动的属性animated值改成NO
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 如果点击的是右边的tableView,不做任何处理 if (tableView == self.rightTableView) return; // 点击左边的tableView,设置选中右边的tableView某一行。左边的tableView的每一行对应右边tableView的每个分区 [self.rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:NO scrollPosition:UITableViewScrollPositionTop]; }
这样做右边的tableView就是无动画滚动了,也就不会再调scrollViewDidScroll:方法。但是如果还想右边tableViewyou滚动效果,另一种解决方法是:把- (void)scrollViewDidScroll:(UIScrollView )scrollView方法换成- (void)scrollViewDidEndDecelerating:(UIScrollView )scrollView这个代理方法方法就行了。
这个方法也有一个bug:
这个方法调用与否在于你的手指是否在动画停止之前离开了屏幕,如果在动画结束之前手指离开屏幕,此方法调用没什么问题。but,如果动画已经停止,再把手指拿开,这个方法是不会调的。
解决这个bug的关键在于,让手指离开的时候手动调一次这个代理方法,那怎么才能知道手指什么时候离开呢?scrollView给我们了另一个代理方法:- (void)scrollViewWillEndDragging:(UIScrollView )scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint )targetContentOffset这个方法在结束拖拽的时候调,正好解决了我们的问题:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{ // 推拽将要结束的时候手动调一下这个方法 [self scrollViewDidEndDecelerating:scrollView]; }
相关文章推荐
- 实现外卖选餐时两级tableView联动效果
- 实现外卖选餐时两级 tableView 联动效果
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- iOS 类似美团外卖app两个tableView联动效果实现
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- IOS实现左右两个TableView联动效果
- 8-20学习练习[用两个tableview实现类似省市联动选择效果]
- 8-20学习练习[用两个tableview实现类似省市联动选择效果]
- 仿zaker最新版本引导界面的视图联动效果(修改viewpager实现)
- 滑动tableView表单时cell图片的视差效果实现分析
- tableview实现qq好友列表点击拉伸的效果
- 使用tableView中的cell来实现单选效果
- tableView:实现导航栏渐变和顶部禁止弹簧效果
- 用collectionView取代tableview更容易实现动画炫酷效果。
- iOS 类似于礼物说分类栏TableView,CollectionView联动的实现
- IOS TableView实现省市联动