IOS个人开发demo技术

iOS-tableView联动你就看我

2016-07-28  本文已影响2228人  麦穗0615

让我们共同学习一下tableView联动,我这也是从简书上看来的一篇文章,来亲自实现一下。学习文章地址:http://www.jianshu.com/p/dfb73aa08602

先上图:

1212.gif
功能需求(两点):
具体思路:

还有一个更简单的方法,在tableView中,是不常用的,叫做,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集合

好的,重点来了,拿到这个集合,不就能拿到目前屏幕上顶端的cell的indexpath了吗,那就如愿以偿的拿到现在所在第indexpath.section个分区了。
上代码:
#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];
}
[详解]:


--------------------------补充-------------------------------

1.点击左边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(估计饿了么没测出来),这个方法的注释为

// called when scroll view grinds to a halt 当滚动视图戛然而止--有道翻译如是说

这个方法调用与否在于你的手指是否在动画停止之前离开了屏幕,如果在动画结束之前手指离开屏幕,此方法调用没什么问题。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];
}

本篇文章,与学习作者文章相同,只不过排版,被我改了,demo中的写法,被我改了。整理一下,学习备用。若有侵权,请及时联系,我会做更改。如有欠缺,或有好的想法,请即时提出。互相交流学习,欢迎点星,鼓励!!!

上一篇 下一篇

猜你喜欢

热点阅读