【问题标题】:NSInvalidArgumentException setSizeHasBeenSet unrecognized selector - with attributed textNSInvalidArgumentException setSizeHasBeenSet 无法识别的选择器 - 带有属性文本
【发布时间】:2017-07-31 08:15:07
【问题描述】:

我正在尝试使用大小调整视图在集合视图中创建动态单元格大小。

如果我使用文本,一切正常,但是当我更改为属性文本时,一切都会崩溃和燃烧。

我有一个带有 UIlabel 的 nib 文件

let sizingView = Bundle.main.loadNibNamed("MyNib", owner: self, options: nil)?[0] as? MyCell

然后我将动态文本发送到单元格

 sizingView.configureCell(promo: promo)

在单元格 I 中

func configureCell(promo:CMSPromotion) {
  if let mainAttributedString = promo.content?.htmlAttributedString() {
    mainText.attributedText = mainAttributedString
  }
}

我的 HTML 属性

// Convert HTML to NSAttributedString
func htmlAttributedString() -> NSMutableAttributedString? {
    guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
    guard let html = try? NSMutableAttributedString(
      data: data,
      options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
      documentAttributes: nil) else { return nil }
    return html
}

我可以在所有 Google 中找到的对 setSizeHasBeenSet 的唯一引用是 _UIFlowLayoutItem 但我不明白为什么这是相关的。

https://github.com/JaviSoto/iOS10-Runtime-Headers/blob/master/Frameworks/UIKit.framework/_UIFlowLayoutItem.h

我在 UICollectionView sizeForItemAt IndexPath 但经过检查,它不是重复的,因为提供的代码有其他问题。

【问题讨论】:

  • 你能显示整个错误信息吗? “发送到实例的无法识别的选择器”给出了调用的方法和不匹配的类对象。还有堆栈跟踪?

标签: ios swift uikit


【解决方案1】:

我有一个解决我的问题的方法。我猜这与计算量太大有关,这就是为什么它适用于常规文本但不适用于属性文本

在加载我的集合视图之前,我预先计算了后台线程中的所有单元格高度,而不是在 sizeForItemAtIndexPath 我生锈从我的数组返回预期的高度

  func collectionView(_ collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:IndexPath) -> CGSize {

【讨论】:

    猜你喜欢
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2012-07-16
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多