博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UITableView获取特定位置的cell
阅读量:6908 次
发布时间:2019-06-27

本文共 2300 字,大约阅读时间需要 7 分钟。

代码地址如下:

http://www.demodashi.com/demo/13307.html

一、tableView双级联动

菜单栏联动.gif

UITableView双级联动.gif

以上两种效果比较类似,实现的关键在于都是需要获得在滑动过程中滑动到tableView顶部的cell的indexPath。

方案一:获得当前可见的所有cell,然后取可见cell数组中的第一个cell就是目标cell,再根据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];    }    }
方案二(推荐使用):利用偏移量!偏移量的值实际上可以代表当时处于tableView顶部的cell在tableView上的相对位置, 那么我们就可以根据偏移量获得处于顶部的cell的indexPath。代码如下
- (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];    }    }

二、 获取处于UITableView中心的cell

获取UITableView中心线cell.gif

获取处于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);}

三、项目结构

dUxjnqAXeLhUcIV8GMd.png

俺目前能想到的也就这了,各位同僚有什么好的想法欢迎在此留言交流??????

strip

iOS UITableView获取特定位置的cell

代码地址如下:

http://www.demodashi.com/demo/13307.html

注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

你可能感兴趣的文章
安装DNS服务器
查看>>
DPM2012学习(一),安装DPM2012
查看>>
文件迁移:将/home迁移到一个独立分区中
查看>>
python脚本按表备份MySQL数据库
查看>>
nio Selector 阻塞 唤醒 原理
查看>>
左旋转字符串
查看>>
android StringBuffer类的使用
查看>>
JSP/Servlet及相关技术详解(二)
查看>>
自动点胶机点胶不良率高是什么原因?
查看>>
Win2003中配置FTP服务,开启防火墙导致客户端无法连接【精华详解】
查看>>
【云计算】Linux从入门到精通
查看>>
oracle 解锁用户
查看>>
Hibernate与Mybatis/iBatis的区别
查看>>
Java源码学习之:Semaphore
查看>>
林仕鼎谈架构设计与架构师
查看>>
操作系统CnetOS_7—systemd管理实践指南
查看>>
cocos2d-x滚屏简单实现
查看>>
我的友情链接
查看>>
ThinkPHP的where方法的in操作符说明
查看>>
Maven的依赖
查看>>