layout - 为什么在 Collection View 中使用 headerReferenceSize 和自调整单元格会导致 iOS 8 崩溃?

标签 layout uicollectionview ios8 xcode6-beta5

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

我想将此作为附件 View 的一部分。

我崩溃了……堆栈跟踪的有趣部分如下:

** 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
  ...

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

最佳答案

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

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

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

关于layout - 为什么在 Collection View 中使用 headerReferenceSize 和自调整单元格会导致 iOS 8 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460323/

相关文章:

ios - iOS 中纵向和横向 View 的不同布局和约束?

java - 无法使该网格正确对齐

ios - 加载 UICollectionView 时崩溃,为什么?

iOS 8 - AutoLayout 问题 - UITableViewCell 中的 UIImageView 忽略宽度限制

ios - 在遗留项目中保留黑色状态栏

java - 如何更改 log4j 中的 htmllayout

Android RelativeLayout 对齐关注

ios - UICollectionView 第一次使用 Swift 水平对齐滚动第一个单元格中心对齐不完全放置中心位置

ios - UICollectionView 自定义单元格布局

ios - 我可以发布以前不支持的iOS版本的更新吗?