【发布时间】:2017-02-03 19:35:41
【问题描述】:
我有一个带有 X 项的 AttributedString 数组和一个将显示该数组的 tableView,并且对于数组中的每个项,表中都会有一个 Section。 在“cellForRowAt”函数中,如果我这样做 "let text = arrayTexts[indexPath.section][indexPath.row]",出现错误'Type NSAttributedString has no subscript members'。 我能做什么?
声明:var arrayTexts = [NSAttributedString]()
填充:
let html = json["data"][order]["informations"][textIndex]["text"].stringValue
let textHtml = html.unescapingFromHTML
do {
let str = try NSMutableAttributedString(data: textHtml.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
let fullRange : NSRange = NSMakeRange(0, str.length)
str.addAttributes([NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 17.0)!], range: fullRange )
self.arrayTexts.append(str)
self.tableView.reloadData()
} catch {
print(error)
}
【问题讨论】:
-
展示你如何声明和填充
arrayTexts。 -
声明:var arrayTexts = [NSAttributedString]()。每个项目都是来自数据库的字符串,我使用“try NSMutableAttributedString (data : , options : , documentAttributes : )”将其转换为属性字符串并附加到 arrayTexts
-
您的语法假定数组(行)位于数组(部分)
[[<Type>]]。 -
@vadian 是的,我尝试不使用 [indexPath.section] 并显示正确数量的部分,但所有部分中的文本都相同