【问题标题】:How to detect a collectionview is out of screen and return it to its initial scroll position?如何检测collectionview不在屏幕上并将其返回到其初始滚动位置?
【发布时间】:2016-04-28 18:43:00
【问题描述】:

我在 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 不在屏幕上时,将其返回到其初始滚动位置以避免崩溃。那么我该如何检测呢?还有其他解决方案吗?

【问题讨论】:

  • 您的表格视图或集合视图一定有问题。此设置应该可以正常运行。
  • 请附上您的 tableview 委托方法和 tableviewcell 源。您的 collectionview 数据源是哪个?尝试设置 collectionview 数据源并委托给 nil prepareToReuse 方法。
  • 在 uitableview 和 uicollectionview 委托方法上显示你的代码...我想你没有传递正确数量的单元格以由 uiCollectionView 创建。 U 正在尝试显示比 UICollectioView 中的单元格数量更多的项目。

标签: ios objective-c swift


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2011-08-03
    • 2012-07-21
    相关资源
    最近更新 更多