3

我正在制作一个获取博客 JSON 内容的应用程序。博客文章的标题显示在 tableView 中。

获取的标题是 HTML 编码的。所以我用这段代码解码了它们

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject

    var encodedString = object.valueForKey("title")!.description
    var encodedData = (encodedString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    var attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
    var attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)

    var decodedString = attributedString.string
    cell.textLabel?.text = decodedString

    // cell.detailTextLabel?.text = object.valueForKey("publishedDate")!.description
}

我可以完成解码,并且标题完美地显示在模拟器中。但控制台显示此错误ThisIsMe[6837:2029906] +[CATransaction synchronize] called within transaction4 次。代码中没有其他错误,所有其他功能都运行良好。

请帮忙

4

1 回答 1

1

这个问题是在解码 HTML 实体时出现的,所以我寻找了另一种解码方式并使用了以下代码:

 func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
                let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject


      let eTitle:NSString = object.valueForKey("title")!.description
      let deTitle  = eTitle.stringByDecodingHTMLEntities()
      cell.textLabel?.text = deTitle
}

早些时候,stringByDecodingHTMLEntities()失踪了。所以我采取了这种方法。

注意:要获取stringByDecodingHTMLEntities()我们需要导入 NSString+HTML.h,从这里 为 HTML 的 NSString 类别

于 2015-02-13T07:26:01.227 回答