以上两种效果比较类似,实现的关键在于都是需要获得在滑动过程中滑动到tableView顶部的cell的indexPath。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == _rightTableView && _isSelected == NO) {
//返回tableView可见的cell数组
NSArray * array = [_rightTableView visibleCells];
//返回cell的IndexPath
NSIndexPath * indexPath = [_rightTableView indexPathForCell:array.firstObject];
NSLog(@"滑到了第 %ld 组 %ld个",indexPath.section, indexPath.row);
_currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[_leftTableView reloadData];
[_leftTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == _rightTableView && _isSelected == NO) {
//系统方法返回处于tableView某坐标处的cell的indexPath
NSIndexPath * indexPath = [_rightTableView indexPathForRowAtPoint:scrollView.contentOffset];
NSLog(@"滑到了第 %ld 组 %ld个",indexPath.section, indexPath.row);
_currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[_leftTableView reloadData];
[_leftTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
}
获取处于tableView中间cell的效果,用上述方案一比较麻烦:要考虑可见cell 的奇、偶个数问题,还有cell是否等高的情况;方案二用起来就快捷方便多了,取的cell的位置的纵坐标相当于在偏移量的基础上又增加了tableView高度的一半。代码如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//获取处于UITableView中心的cell
//系统方法返回处于tableView某坐标处的cell的indexPath
NSIndexPath * middleIndexPath = [_rightTableView indexPathForRowAtPoint:CGPointMake(0, scrollView.contentOffset.y + _rightTableView.frame.size.height/2)];
NSLog(@"中间的cell:第 %ld 组 %ld个",middleIndexPath.section, middleIndexPath.row);
}
需要示例Demo的话请移驾我的Github→UITableViewLinkage
俺目前能想到的也就这了,各位同僚有什么好的想法欢迎在此留言交流😀😁😀👏👏👏
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有