zoukankan      html  css  js  c++  java
  • scrollToItemAtIndexPath: atScrollPosition: animated:

    oh my god

    今天死在scrollToItemAtIndexPath: atScrollPosition: animated:方法上面,scrollPosition这个参数控制cell具体停留在上下左右中到底哪个位置,看了API有7个枚举值

    UICollectionViewScrollPositionNone                 = 0,
        
    // The vertical positions are mutually exclusive to each other, but are bitwise or-able with the horizontal scroll positions.
    // Combining positions from the same grouping (horizontal or vertical) will result in an NSInvalidArgumentException.
        UICollectionViewScrollPositionTop                  = 1 << 0,
        UICollectionViewScrollPositionCenteredVertically   = 1 << 1,
        UICollectionViewScrollPositionBottom               = 1 << 2,
        
    // Likewise, the horizontal positions are mutually exclusive to each other.
        UICollectionViewScrollPositionLeft                 = 1 << 3,
        UICollectionViewScrollPositionCenteredHorizontally = 1 << 4,
        UICollectionViewScrollPositionRight                = 1 << 5

    之前没有去理解过它几个参数的意思,要实现在二级界面通过indexPath获取到一级界面的collectionView的对应位置的cell

    UIView *sourceCell = [_sourceView cellForItemAtIndexPath:indexPath];
    if (!sourceCell) {
        [_sourceView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
        [_sourceView layoutIfNeeded];
        sourceCell = [_sourceView cellForItemAtIndexPath:indexPath];
    }

    此时cell存在nil的情况,collectionView.visibleCells没有这个cell,需要从重用池获取,所以要滚动到这个cell位置后走layoutSubViews布局,这样才拿的到cell

    在给参数时给了 UICollectionViewScrollPositionLeft  ,然后pop回去就一直没效果,用了代理 block都还是不行,以为是layoutIfNeeded的问题搞了很久。

    最后 给 UICollectionViewScrollPositionCenteredVertically 这个就成了。

    根据API的解释 分成水平滚动和垂直滚动

    可以想象的是当collectionView水平滚动时cell的定点相对于整个collectionView的左中右位置

                                         当垂直滚动时cell的定点相对于整个collectionView的上中下位置

  • 相关阅读:
    近来有客户要求用table显示一大串数据,由于滚动后就看不到表头,很不方便,所以想到这个效果。
    js如何取当前日期时间/格式为:yyyymmdd hh:mm:ss
    jdk源码分析 – Thread线程类源码分析
    Swing中的MVC
    编程生涯的一次思想总结
    怎样成为高手
    浅谈测试驱动开发(TDD)
    Java RMI之HelloWorld篇(EJB都是建立在rmi基础之上的)
    Swing框架之Model
    EJB 工作原理
  • 原文地址:https://www.cnblogs.com/xs514521/p/5761831.html
Copyright © 2011-2022 走看看