我正在尝试使用 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()
}
}
ColumnHeaderView nib 实际上是一个 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
为什么会这样?我错过了什么吗?
任何帮助都会得到帮助:) 提前谢谢你
编辑:我发现了问题!这一行是错误的:
UICollectionViewLayoutAttributes(forDecorationViewOfKind: elementKind, withIndexPath: indexPath)
我试图获取装饰视图的 ViewLayoutAttributes,但它被注册为可重用视图。我改变了这条线,它又可以工作了。