2014-09-22 112 views
2

我想使用創建一個使用UICollectionViewController的iCalendar lookalike。現在我正在嘗試爲每天的列標題添加可重複使用的視圖。iOS Swift UICollectionView registerNib可重用視圖不工作

我由故事板並添加鏈接到這個類UICollectionViewController:

import UIKit 

class WeekViewController: UICollectionViewController, UICollectionViewDelegate 
{ 
    let dataSource = WeekViewDataSource() 
    let weekLayout = WeekViewLayout() 

    override func awakeFromNib() { 
     var columnHeaderViewNIB = UINib(nibName: "ColumnHeaderView", bundle: nil) 
     self.collectionView?.registerNib(columnHeaderViewNIB, forSupplementaryViewOfKind: columnHeaderViewIdentifier, withReuseIdentifier: columnHeaderViewIdentifier) 

     self.collectionView?.collectionViewLayout = weekLayout 
     self.collectionView?.dataSource = dataSource 

     super.awakeFromNib() 
    } 
} 

的ColumnHeaderView筆尖實際上是與含有單個標籤的視圖一個XIB文件。這種觀點被鏈接到這個類:

import UIKit 

class ColumnHeaderView: UICollectionReusableView 
{ 
    @IBOutlet weak var label: UILabel! 
} 

數據源和佈局:

import UIKit 

class WeekViewDataSource: NSObject, UICollectionViewDataSource { 

    var events = Array<String>() 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
     return 0 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     return UICollectionViewCell() 
    } 

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 
     var view: UICollectionReusableView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: kind, forIndexPath: indexPath) as UICollectionReusableView 

     configureHeader(view, kind: kind, indexPath: indexPath) 

     return view 
    } 

    func configureHeader(view: UICollectionReusableView, kind: String, indexPath: NSIndexPath) 
    { 
     if kind == columnHeaderViewIdentifier 
     { 
      let interval = NSTimeInterval(indexPath.item * 24 * 60 * 60) 
      let now = NSDate().dateByAddingTimeInterval(interval) 
      let format = NSDateFormatter() 
      format.dateStyle = NSDateFormatterStyle.MediumStyle 
      format.timeStyle = NSDateFormatterStyle.MediumStyle 
      format.locale = NSLocale() 
      format.dateFormat = "EE\ndd. MMMM" 

      let headerView: ColumnHeaderView = view as ColumnHeaderView 

      headerView.label.text = format.stringFromDate(now) 
     } 
    } 
} 




class WeekViewLayout: UICollectionViewLayout 
{ 
    var DaysPerWeek = 7 
    var HoursPerDay = 24 
    var HeightPerHour: CGFloat = 60.0 
    var DayHeaderHeight: CGFloat = 100.0 
    var DayHeaderWidth: CGFloat = 120.0 
    var HourHeaderWidth: CGFloat = 50.0 

    override func collectionViewContentSize() -> CGSize { 
     var contentWidth = HourHeaderWidth + DayHeaderHeight * CGFloat(DaysPerWeek) 
     var contentHeight = DayHeaderHeight + HeightPerHour * CGFloat(HoursPerDay) 

     return CGSizeMake(contentWidth, contentHeight) 
    } 

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? { 
     var layoutAttributes = Array<UICollectionViewLayoutAttributes>() 

     // Column Header - Days 
     var dayHeaderViewIndexPaths = indexPathsOfDayHeaderViewsInRect(rect) 
     for indexPath in dayHeaderViewIndexPaths 
     { 
      var attributes = layoutAttributesForSupplementaryViewOfKind(columnHeaderViewIdentifier, atIndexPath: indexPath) 
      layoutAttributes.append(attributes) 
     } 

     return layoutAttributes 
    } 

    override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! { 
     var attributes = UICollectionViewLayoutAttributes(forDecorationViewOfKind: elementKind, withIndexPath: indexPath) 

     if elementKind == columnHeaderViewIdentifier 
     { 
      attributes.frame = CGRectMake(
       HourHeaderWidth + DayHeaderWidth * CGFloat(indexPath.length), 
       self.collectionView!.contentOffset.y, 
       DayHeaderWidth, 
       DayHeaderHeight) 
     } 

     return attributes 
    } 


    func indexPathsOfDayHeaderViewsInRect(frame: CGRect) -> [NSIndexPath] 
    { 
     var minDayIndex = dayIndexFromXCoord(CGRectGetMinX(frame)) 
     var maxDayIndex = dayIndexFromXCoord(CGRectGetMaxX(frame)) 

     var indexPaths = Array<NSIndexPath>() 
     for i in (minDayIndex...maxDayIndex) 
     { 
      indexPaths.append(NSIndexPath(index: i)) 
     } 

     return indexPaths 
    } 


    /* 
     Helper methods 
    */ 

    func dayIndexFromXCoord(positionX: CGFloat) -> Int 
    { 
     var contentWidth = collectionViewContentSize().width - HourHeaderWidth 
     var widthPerDay = contentWidth/CGFloat(DaysPerWeek) 
     return Int(max(0.0, (positionX - HourHeaderWidth)/widthPerDay)) 
    } 
} 

我不是那麼遠,因爲我被困在一個非常基本的問題。

The ViewController doesn't register the nib and I always get this error: 
2014-09-22 22:49:52.242 WeekView[4973:304060] *** Assertion failure in -[WeekView.WeekViewLayout _decorationViewForLayoutAttributes:], /SourceCache/UIKit_Sim/UIKit-3318/UICollectionViewLayout.m:1292 
2014-09-22 22:49:52.244 WeekView[4973:304060] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a decoration view of kind: ColumnHeaderView - must register as a class or nib or connect a prototype in a storyboard' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000100aa13f5 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000102812bb7 objc_exception_throw + 45 
    2 CoreFoundation      0x0000000100aa125a +[NSException raise:format:arguments:] + 106 
    3 Foundation       0x000000010117b28f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 
    4 UIKit        0x0000000101bb699a -[UICollectionViewLayout _decorationViewForLayoutAttributes:] + 872 
    5 UIKit        0x0000000101b982e6 -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:] + 413 
    6 UIKit        0x0000000101b994da -[UICollectionView _updateVisibleCellsNow:] + 3483 
    7 UIKit        0x0000000101b9d161 -[UICollectionView layoutSubviews] + 243 
    8 UIKit        0x00000001015e7199 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521 
    9 QuartzCore       0x0000000105f6cf98 -[CALayer layoutSublayers] + 150 
    10 QuartzCore       0x0000000105f61bbe _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 
    11 QuartzCore       0x0000000105f61a2e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    12 QuartzCore       0x0000000105ecfade _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 
    13 QuartzCore       0x0000000105ed0bea _ZN2CA11Transaction6commitEv + 390 
    14 UIKit        0x000000010156c67d -[UIApplication _reportMainSceneUpdateFinished:] + 44 
    15 UIKit        0x000000010156d368 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642 
    16 UIKit        0x000000010156bd22 -[UIApplication workspaceDidEndTransaction:] + 179 
    17 FrontBoardServices     0x000000010507d2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16 
    18 CoreFoundation      0x00000001009d6abc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12 
    19 CoreFoundation      0x00000001009cc805 __CFRunLoopDoBlocks + 341 
    20 CoreFoundation      0x00000001009cbfc3 __CFRunLoopRun + 851 
    21 CoreFoundation      0x00000001009cba06 CFRunLoopRunSpecific + 470 
    22 UIKit        0x000000010156b799 -[UIApplication _run] + 413 
    23 UIKit        0x000000010156e550 UIApplicationMain + 1282 
    24 WeekView       0x00000001008afc0e top_level_code + 78 
    25 WeekView       0x00000001008afc4a main + 42 
    26 libdyld.dylib      0x0000000102fdd145 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

這是怎麼發生的? 我錯過了什麼嗎?

任何幫助,將appreachiated :)謝謝你在前進

編輯: 我發現這個問題! 此行是錯誤的:

UICollectionViewLayoutAttributes(forDecorationViewOfKind: elementKind, withIndexPath: indexPath) 

我試圖讓一個裝飾鑑於ViewLayoutAttributes但它註冊爲可重複使用的視圖。我改變了這條線,它再次運作。

+0

你如何改變你的生產線?我有同樣的問題 – 2014-12-03 11:30:02

+0

我將它更改爲UICollectionViewLayoutAttributes(forSupplementaryViewOfKind:elementKind,withIndexPath:indexPath),因爲它是一個補充視圖而不是裝飾視圖 – 2014-12-07 18:42:52

回答

1

這是因爲您在調用registerNib方法時未設置您的插座。將您的代碼移動到viewDidLoad。由於self.collectionView是可選類型的swift編譯器無法捕捉到。

+0

我將它移動到viewDidLoad併發生同樣的錯誤。我添加了一個打印語句並打印出self.collectionView,並且它已經被分配,並且可能在該點處初始化: 可選(; layer = ; contentOffset:{0,0}; contentSize:{0,0}>集合視圖佈局:) – 2014-09-24 04:58:17

0

這樣做的原因是,你的佈局請求佈局的裝飾查看屬性,但你註冊NIB的補充視圖。這可能有點令人困惑,因爲UICollectionViewLayout對象在創建裝飾視圖時不符合其代表,它只是在知道其物理外觀不會被集合內容告知的情況下創建它們。

由於這個原因,裝飾視圖與UICollectionViewLayout函數registerNib: forDecorationViewOfKind:而不是UICollectionView本身註冊。