如何检测集合视图是否在屏幕外并将其返回到其初始滚动位置?

时间:2016-01-22 08:24:26

标签: ios objective-c swift

我在TableView中遇到CollectionView问题。 CollectionView包含一堆操作按钮。您可以水平滚动它们。 Tableview包含一些其他视图和我们的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在屏幕外时,将其返回到其初始滚动位置以避免崩溃。那我怎么检测到呢?还有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

我最终实现了didEndDisplayingCell并且它有效。这是解决方案:

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

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