可重用视图的iOS Swift UICollectionView注册表无效

可重用视图的iOS Swift UICollectionView注册表无效,ios,swift,nib,collectionview,reuseidentifier,Ios,Swift,Nib,Collectionview,Reuseidentifier,我正在尝试使用UICollectionViewController创建一个类似iCalendar的外观。现在,我正在尝试添加可重用视图作为每天的列标题 我制作了一个故事板,并添加了一个链接到此类的UICollectionViewController: import UIKit class WeekViewController: UICollectionViewController, UICollectionViewDelegate { let dataSource = WeekView

我正在尝试使用UICollectionViewController创建一个类似iCalendar的外观。现在,我正在尝试添加可重用视图作为每天的列标题

我制作了一个故事板,并添加了一个链接到此类的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()
    }
}
import UIKit

class ColumnHeaderView: UICollectionReusableView
{
    @IBOutlet weak var label: UILabel!
}
ColumnHeaderView nib实际上是一个包含单个标签的视图的xib文件。此视图链接到此类:

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()
    }
}
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))
    }
}
为什么会这样? 我遗漏了什么吗

任何帮助都会被告知:提前谢谢你

编辑: 我发现了问题! 这句话错了:

UICollectionViewLayoutAttributes(forDecorationViewOfKind: elementKind, withIndexPath: indexPath)

我试图获取装饰视图的ViewLayoutAttribute,但它已注册为可重用视图。我更改了这一行,它又工作了。

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

原因是您的布局正在请求装饰视图的布局属性,但您正在为补充视图注册NIB。这可能有点混乱,因为UICollectionViewLayout对象在创建装饰视图时与其委托不对应,它只是在知道其物理外观不受集合内容通知的情况下创建它们


因此,装饰视图是使用UICollectionViewLayout函数注册表nb:forDecorationViewOfKind:而不是UICollectionView本身注册的。

我将其移动到viewDidLoad,同样的错误也会发生。我添加了一个print语句并打印了self.collectionView,它已经被分配了,可能也在那个时候初始化了:可选的collection视图布局:如何更改行?我也有同样的问题,我将其更改为UICollectionViewLayoutAttributes for supplementary视图of Kind:elementKind,带有indexPath:indexPath,因为它是补充视图而不是装饰视图