ios - 如何检测 collectionview 超出屏幕并将其返回到其初始滚动位置?

标签 ios objective-c swift

我在使用 TableView 中的 CollectionView 时遇到问题。 CollectionView 包含一堆操作按钮。您可以水平滚动浏览它们。 Tableview 包含一些其他 View 和我们的 CollectionView。您可以垂直滚动此 TableView。

它工作正常,直到您滚动 CollectionView 一点,保持原样,然后滚动主 TableView。在 TableView 单元格离开屏幕后,应用程序崩溃并显示以下消息:

2016-01-22 10:19:30.008 Cool[1807:31453] *** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UICollectionViewData.m:399
2016-01-22 10:19:30.052 Cool[1807:31453] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x7ae57380> {length = 2, path = 0 - 3}'
*** First throw call stack:
(
    0   CoreFoundation                      0x00aa2a84 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x02b7ee02 objc_exception_throw + 50
    2   CoreFoundation                      0x00aa291a +[NSException raise:format:arguments:] + 138
    3   Foundation                          0x010e4e86 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
    4   UIKit                               0x02081c79 __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 1116
    5   UIKit                               0x02080c1f -[UICollectionViewData validateLayoutInRect:] + 1329
    6   UIKit                               0x0202694b -[UICollectionView layoutSubviews] + 188
    7   UIKit                               0x01790eb7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
    8   libobjc.A.dylib                     0x02b93059 -[NSObject performSelector:withObject:] + 70
    9   QuartzCore                          0x0159280a -[CALayer layoutSublayers] + 144
    10  QuartzCore                          0x015864ee _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
    11  QuartzCore                          0x01586352 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    12  QuartzCore                          0x01578e8b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
    13  QuartzCore                          0x015ace03 _ZN2CA11Transaction6commitEv + 561
    14  QuartzCore                          0x015ae674 _ZN2CA11Transaction17flush_transactionEv + 50
    15  QuartzCore                          0x015bdc4f _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 593
    16  QuartzCore                          0x015be0b5 _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 123
    17  CoreFoundation                      0x009f4576 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
    18  CoreFoundation                      0x009f3f72 __CFRunLoopDoTimer + 1250
    19  CoreFoundation                      0x009b225a __CFRunLoopRun + 2202
    20  CoreFoundation                      0x009b1706 CFRunLoopRunSpecific + 470
    21  CoreFoundation                      0x009b151b CFRunLoopRunInMode + 123
    22  GraphicsServices                    0x04486664 GSEventRunModal + 192
    23  GraphicsServices                    0x044864a1 GSEventRun + 104
    24  UIKit                               0x016c71eb UIApplicationMain + 160
    25  Cool                                0x00036ddc main + 140
    26  libdyld.dylib                       0x0729aa21 start + 1
    27  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我的想法是,当 CollectionView 超出屏幕时,将其返回到其初始滚动位置以避免崩溃。那么我该如何检测呢?还有其他解决方案吗?

最佳答案

我最终实现了 didEndDisplayingCell 并且它起作用了。这是解决方案:

override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

            let mycell = cell as! MyCell
            mycell.collectionView.setContentOffset(CGPointZero, animated: false)
        }

关于ios - 如何检测 collectionview 超出屏幕并将其返回到其初始滚动位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34942073/

相关文章:

ios - Alamofire 版本 5.2.0 和 5.2.1 都在 URL session 委托(delegate)时崩溃

ios - NULL 空操作在 GCC/Clang 上编译成什么?

ios - 如何使 UITableView 标题可选择?

objective-c - 学习NSBlockOperation

objective-c - 如何在 NSPoint 上定义基本的几何运算?

swift - 有没有办法查看程序集文件?

ios - 在 Uitextfield 上使用 UiSearchBar 有任何真正的理由吗?

objective-c - 在方法内部分配初始化

c++ - 在 Xcode 中将 C++ 与 OpenGL 结合使用

ios - 将 View Controller View 添加到 subview 时发现 nil