iOSCollectioView滚动到指定section的方法
CollectioView滚动到指定section的方法
项目中的需求:collectionView顶部有一个scrollView组成的标签,点击标签,让collectionView滚动到指定的行,滚动collectionView自动切换到顶部指定的标签
实现方法如下:
1. 保证collectionView全部加载完毕,我这里通过一个bool的标志位来标示
  -(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  {
        if([indexPath row] == ((NSIndexPath*)[[collectionView indexPathsForVisibleItems] lastObject]).row){
            self.isLoaded = YES;
        }
   }
2. 标签点击了CollectionView滚动到指定行
        if (self.isLoaded) {
            // scroll to selected index
            NSIndexPath* cellIndexPath = [NSIndexPath indexPathForItem:0 inSection:index+1];
            UICollectionViewLayoutAttributes* attr = [self.collectionView.collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:cellIndexPath];
            UIEdgeInsets insets = self.collectionView.scrollIndicatorInsets;
            CGRect rect = attr.frame;
            rect.size = self.collectionView.frame.size;
            rect.size.height -= insets.top + insets.bottom;
            CGFloat offset = (rect.origin.y + rect.size.height) - self.collectionView.contentSize.height;
            if ( offset > 0.0 ) rect = CGRectOffset(rect, 0, -offset);
            [weakSelf.collectionView scrollRectToVisible:rect animated:YES];
        }
3. CollectionView滚动到指定行的时候,同时切换标签滚动
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        static NSInteger currentIndex = 0;
        NSInteger index=scrollView.contentOffset.y;
        CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
        CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
        NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
//        NSLog(@"%@",visibleIndexPath);
        if (currentIndex == visibleIndexPath.section || visibleIndexPath == nil) {
            return;
        }
        currentIndex = visibleIndexPath.section;
       [self.itemTool itemScrollToPositionWithIndex:currentIndex isJump:YES];
    }
iOSCollectioView滚动到指定section的方法的更多相关文章
- CollectioView滚动到指定section的方法
		
项目中的需求:collectionView顶部有一个scrollView组成的标签,点击标签,让collectionView滚动到指定的行,滚动collectionView自动切换到顶部指定的标签 实 ...
 - jQuery实现将div中滚动条滚动到指定位置的方法
		
1.JS代码: onload = function () { //初始化 scrollToLocation(); }; function scrollToLocation() { var mainCo ...
 - js将滚动条滚动到指定位置的方法
		
代码如下(主要是通过设置Location的hash属性): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...
 - 使用JS方法使页面滚动到指定元素+优化+API介绍(动画)
		
前言 当页面最上部有顶部菜单是,使用锚点跳转的方法很容易挡住想要呈现的内容(如下图技能两个字被挡住了一半),为避免出现这样的问题,故滚动到指定元素使用用JS的方法来实现. 目录 使用的API简介 初版 ...
 - tableView刷新指定的cell 或section和滚动到指定的位置
		
转自:http://blog.csdn.net/tianyou_code/article/details/54426494 //一个section刷新 NSIndexSet *indexSet=[[N ...
 - js滚动到指定位置
		
序言:在网络上百度,关键字:“js div滚动到指定位置”,结果基本上大同小异!各种大神都给我们总结出来了四种滚动到指定位置的办法,可惜再下愚钝,每个都不会用,所以写了一个超级简单的方法来使初学者一看 ...
 - Android ListView滚动到指定的位置
		
这篇文章主要给大家介绍了Android中的ListView如何滚动到指定的位置,文章给出了两种解决的方法,并给出详细的示例代码,相信会对大家的理解和学习很有帮助,有需要的朋友们下面来一起看看吧. 本文 ...
 - iOS - UITableView滚动到指定的cell并且选中
		
UITableView //项目中遇到的 - (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)a ...
 - selenium webdriver——JS滚动到指定位置
		
1.DOM滚动方法 1.scrollIntoView(alignWithTop) 滚动浏览器窗口或容器元素,以便在当前视窗的可见范围看见当前元素.如果alignWithTop为true,或者省略它, ...
 
随机推荐
- jQuery 自定义网页滚动条样式插件 mCustomScrollbar 的介绍和使用方法(转)
			
系统默认的滚动条样式,真的已经看的够恶心了.试想一下,如果在一个很有特色和创意的网页中,出现了一根系统中默认的滚动条样式,会有多么的别扭. 为了自己定义网页中的滚动条的方法,我真的已经找了很久了,就目 ...
 - iscsi target 之LIO配置
			
LIO 配置 现在主流Linux都可以设置iSCSI,如STGT/TGT.LIO Target等.Linux-IO(LIO)Target是当前Linux标准的iSCSI target的开源实现,包含在 ...
 - io分析神器blktrace
			
一.概述 [许久之前就用过blktrace,现整理如下] 从linux 一个完整的IO入手分析: 一个I/O请求进入block layer之后,可能会经历下面的过程: Remap: 可能被DM(Dev ...
 - 【Hibernate】数据Session对象的常规操作收集
			
因为Hibernate是ORM(对象关系映射)的,所以程序员是不需要写Sql语句的.所有的操作都是通过对对象的操作. 1,原生Session 事务管理 Transaction tx = session ...
 - mysql使用GROUP BY分组实现取前N条记录的方法
			
MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY分组取前N条记录实现方法. 这是测试表(也不知道 ...
 - response.encodeURL的用法
			
Java Servlet API 中引用 Session 机制来追踪客户的状态.Servlet API 中定义了 javax.servlet.http.HttpSession 接口,Servlet 容 ...
 - Java 垃圾回收思维导图
			
文 by / 林本托 Tips 做一个终身学习的人. Java 的垃圾回收,不像 C和 C++语言,内存的分配和释放都是靠程序员来控制的.而 Java 的内存回收,程序员是不能也是无法干预,具体什么时 ...
 - 第一篇:初识ASP.NET控件开发_第二节:HelloWorld
			
1)步骤一:新建类库项目:Controls,创建新解决方案:CustomLibrary 2)步骤二:在类库项目中添加“ASP.NET服务器控件”新建项:RenderHelloWorld.cs (也可以 ...
 - Java 8 – Convert Instant to ZonedDateTime
			
1. Instant -> ZonedDateTime Example to convert a Instant UTC+0 to a Japan ZonedDateTime UTC+9 Ins ...
 - Spring boot注解(annotation)含义详解
			
Spring boot注解(annotation)含义详解 @Service用于标注业务层组件@Controller用于标注控制层组件(如struts中的action)@Repository用于标注数 ...