Search code examples
iosswifttableviewuitableview

Set UITableViewCell label to be generated from HTML string


I'm using TableView on swift and I would like to know how can I set the UITableViewCell label to be generated from HTML string.

I know how to use it to present text on the cell label:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    var articlesInSection = xmlParser.channelsArray[indexPath.section]["articles"] as [AnyObject]

    //articleWebView.loadHTMLString(activeItem, baseURL: nil)

    cell.textLabel?.text = articlesInSection[indexPath.row]["title"] as NSString

    return cell
}

but I have an HTML I want to present instead of the text, something similar to loadHTMLString function

How can I do it?


Solution

  • You could use NSAttributedString to show text from html but I don't think this is very efficient solution inside table view cell because it can cause lag while scrolling and/or delay while loading

    var attrStr = NSAttributedString(
                data:yourHTMLString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
                options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                documentAttributes: nil,
                error: nil)
    cell.textLabel?.attributedText = attrStr