【问题标题】:Why does using headerReferenceSize with self-sizing cells in a collection view cause a crash in iOS 8?为什么在集合视图中使用带有自调整单元格的 headerReferenceSize 会导致 iOS 8 崩溃?
【发布时间】:2014-10-17 02:14:15
【问题描述】:

我设法弄清楚the approach for self-sizing collection view cells under iOS 8

我想将其作为附件视图的一部分。

我遇到了崩溃...堆栈跟踪中有趣的部分如下:

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
  0   CoreFoundation    __exceptionPreprocess + 165
  1   libobjc.A.dylib   objc_exception_throw + 45
  2   CoreFoundation    -[__NSArrayM insertObject:atIndex:] + 954
  3   UIKit             -[UICollectionViewFlowLayout layoutAttributesForElementsInRect:] + 384
  4   UIKit             __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 144
  5   UIKit             -[UICollectionViewData validateLayoutInRect:] + 1396
  6   UIKit             -[UICollectionView layoutSubviews] + 170
  7   UIKit             -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
  8   QuartzCore        -[CALayer layoutSublayers] + 150
  9   QuartzCore        _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
  10  UIKit             -[UIView(Hierarchy) layoutBelowIfNeeded] + 611
  11  UIKit             -[UIInputSetHostView layoutIfNeeded] + 105
  12  UIKit             __43-[UIInputWindowController setInputViewSet:]_block_invoke + 112
  13  UIKit             +[UIView(Animation) performWithoutAnimation:] + 65
  14  UIKit             -[UIInputWindowController setInputViewSet:] + 291
  15  UIKit             -[UIInputWindowController performOperations:withAnimationStyle:] + 50
  16  UIKit             -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1054
  17  UIKit             -[UIResponder becomeFirstResponder] + 468
  18  UIKit             -[UIView(Hierarchy) becomeFirstResponder] + 99
  19  UIKit             -[UITextView becomeFirstResponder] + 75
  ...

这看起来像一个 Apple 错误。为什么布局似乎返回一个空的布局属性?

【问题讨论】:

  • 如果我设置一个带有一些固定项目大小的标题,它工作正常。如果我改为使用estimatedItemSize,则会发生此错误。我的结论是 estimatedItemSize 有效,但不适用于 headerReferenceSize
  • 您是否为此提交了雷达?
  • 有没有人设法找到解决方法?就目前而言,我无法使用具有自定尺寸布局的标题。 @fatuhoko,当你说“如果我设置了一个具有固定项目大小的标题,它工作正常” - 你是怎么做到的?
  • 我的意思是,如果您忘记使用estimatedItemSize 来完全调整单元格大小,而只是通过设置itemSize 属性来调整单元格大小,那么您的标题就可以工作了。即使用该属性或覆盖itemSizeForIndexPath 或其他任何一个的旧功能仍然有效。
  • 有人找到解决方法了吗?

标签: layout uicollectionview ios8 xcode6-beta5


【解决方案1】:

到目前为止我最好的想法是基于这个存储库:https://github.com/algal/SelfSizingCellsDemo

在 ViewController.swift 的第 50 行,我们有label.preferredMaxLayoutWidth = 320,它会生成一个填满整个屏幕的单元格,如果有足够的文本,则会换行。这需要更改以适合您正在使用的任何尺寸的屏幕。然后在每个部分之后,您需要添加足够的文本来填充该标签,就像在第 20 行完成的 let items = smallitems.componentsSeparatedByString(" ") + [onelongitem]

这种方法的问题在于,除了文本之外,我还不知道如何在该单元格中放置不同的视图。不过,这可能足以满足您的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    相关资源
    最近更新 更多