Rodrigo Fuscaldi
Rodrigo Fuscaldi

Reputation: 182

Type NSAttributedString has no subscript members in tableView

I have an array of AttributedString with X items and a tableView that will display this array, and for each item in the array there will be a Section in the table. In the "cellForRowAt" function, if I do "let text = arrayTexts[indexPath.section][indexPath.row]", the error 'Type NSAttributedString has no subscript members' appears. What can I do?

Declaration: var arrayTexts = [NSAttributedString]()

Populate:

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)
                            }

Upvotes: 2

Views: 385

Answers (2)

creeperspeak
creeperspeak

Reputation: 5523

The problem is that your let text = arrayTexts[indexPath.section][indexPath.row] will only work if you've got an array of arrays of NSAttributedStrings ([[NSAttributedString]]). So arrayTexts[indexPath.section] will return a single NSAttributedString, so trying to subscript that with [indexPath.row] will fail. It's hard to understand exactly what you're trying to do without seeing more code, but from your description it sounds like you can just change the code to let text = arrayTexts[indexPath.section] and it'll give you what you want.

Upvotes: 1

vadian
vadian

Reputation: 285059

You need to declare the data source array

var arrayTexts = [[NSAttributedString]]()

and populate the array

self.arrayTexts.append([str])

That creates a section for each item.

Upvotes: 1

Related Questions