2016-01-22 50 views
2

我在一個TableView中有一個CollectionView的問題。 CollectionView包含一堆操作按鈕。您可以水平滾動瀏覽它們。 Tableview包含一些其他視圖和我們的CollectionView。您可以垂直滾動此TableView。如何檢測collectionview是否在屏幕之外並將其返回到其初始滾動位置?

它工作正常,直到你滾動一個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是出入畫面時,其返回到其初始滾動位置,以避免崩潰。那麼如何檢測?還有其他解決方案嗎?

+0

您的表格視圖或集合視圖必定有問題。這個設置應該沒有問題。 – kientux

+0

請附上您的tableview委託方法和tableviewcell來源。哪一個是你的collectionview數據源?嘗試通過設置collectionview數據源和委託爲nil prepareToReuse方法。 –

+0

在uitableview和uicollectionview委託方法上顯示你的代碼... 我想你還沒有通過由uiCollectionView創建的單元格的數量。 U正嘗試顯示比UICollectioView中的單元數目多的項目。 – Shravan

回答

0

我結束了實現didEndDisplayingCell,它工作。以下是解決方案:

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

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