草庐IT

iOS Swift UICollectionView registerNib 可重用 View 不起作用

coder 2023-09-14 原文

我正在尝试使用 UICollectionViewController 创建一个类似 iCalendar 的对象。现在我正在尝试添加可重复使用的 View 作为每一天的列标题。

我制作了一个 Storyboard并添加了一个链接到此类的 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 实际上是一个包含单个标签的 View 的 xib 文件。此 View 链接到此类:

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)

我试图获取装饰 View 的 ViewLayoutAttributes,但它被注册为可重用 View 。我更改了这一行,它再次起作用。

最佳答案

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

因此,装饰 View 是使用 UICollectionViewLayout 函数 registerNib: forDecorationViewOfKind: 注册的,而不是 UICollectionView 本身。

关于iOS Swift UICollectionView registerNib 可重用 View 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25982931/

有关iOS Swift UICollectionView registerNib 可重用 View 不起作用的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  4. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  5. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  6. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  7. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  8. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  9. ruby - Sinatra 找不到 View 目录 - 2

    我正在尝试以一种更类似于普通RubyGem结构的方式构建我的Sinatra应用程序。我有以下文件树:.├──app.rb├──config.ru├──Gemfile├──Gemfile.lock├──helpers│  ├──dbconfig.rb│  ├──functions.rb│  └──init.rb├──hidden│  └──Rakefile├──lib│  ├──admin.rb│  ├──api.rb│  ├──indexer.rb│  ├──init.rb│  └──magnet.rb├──models│  ├──init.rb│  ├──invite.rb│  ├─

  10. ruby-on-rails - 如何让 Rails View 返回其关联的操作名称? - 2

    我有一个非常简单的Controller来管理我的Rails应用程序中的静态页面:classPagesController我怎样才能让View模板返回它自己的名字,这样我就可以做这样的事情:#pricing.html.erb#-->"Pricing"感谢您的帮助。 最佳答案 4.3RoutingParametersTheparamshashwillalwayscontainthe:controllerand:actionkeys,butyoushouldusethemethodscontroller_nameandaction_nam

随机推荐