iOS Swift UICollectionView registerNib可重用视图不工作

问题描述:

我想使用创建一个使用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

这是因为您在调用registerNib方法时未设置您的插座。将您的代码移动到viewDidLoad。由于self.collectionView是可选类型的swift编译器无法捕捉到。

+0

我将它移动到viewDidLoad并发生同样的错误。我添加了一个打印语句并打印出self.collectionView,并且它已经被分配,并且可能在该点处初始化: 可选(; layer = ; contentOffset:{0,0}; contentSize:{0,0}>集合视图布局:) – 2014-09-24 04:58:17

这样做的原因是,你的布局请求布局的装饰查看属性,但你注册NIB的补充视图。这可能有点令人困惑,因为UICollectionViewLayout对象在创建装饰视图时不符合其代表,它只是在知道其物理外观不会被集合内容告知的情况下创建它们。

由于这个原因,装饰视图与UICollectionViewLayout函数registerNib: forDecorationViewOfKind:而不是UICollectionView本身注册。