首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用WebKit在HTML中显示更多的JSON项?

如何使用WebKit在HTML中显示更多的JSON项?
EN

Stack Overflow用户
提问于 2018-07-03 06:53:29
回答 1查看 119关注 0票数 1

我正在使用SwiftyJSON构建一个新闻应用程序,并且我已经能够正确地提取数据。我还可以在tableView中显示标题和描述。但是,当我转到详细视图时,我希望能够显示提要中的完整文章摘要。

以下是提要元素:

代码语言:javascript
复制
func parse(json: JSON) {
        for article in json["articles"].arrayValue {
            let title = article["title"].stringValue
            let author = article["author"].stringValue
            let date = article["publishedAt"].stringValue
            let image = article["urlToImage"].stringValue
            let description = article["description"].stringValue

            let obj = ["title": title, "author": author, "date": date, "image": image, "description": description, ]
            news.append(obj)

        }

我将数据发送到Detail View Controller,如下所示:

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = DetailViewController()
        vc.articleSummary = news[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
}

然后在细节视图控制器上,这里是代码。注释项目是我想要添加到显示中的项目:

代码语言:javascript
复制
import UIKit
import WebKit

class DetailViewController: UIViewController {

    var webView: WKWebView!
    var articleSummary: [String: String]!

    override func loadView() {
        webView = WKWebView()
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        guard articleSummary != nil else { return }

        if let description = articleSummary["description"] {
            var html = "<html>"
            html += "<head>"
            html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
            html += "<style> body { font-size: 150%; } </style>"
            html += "</head>"
            html += "<body>"
         // html += <h1>title</h1>
         // html += <h4>author</h4>
         // html += <p>date</p>
         // html += <img src="image" alt="" />
            html += description
            html += "</body>"
            html += "</html>"
            webView.loadHTMLString(html, baseURL: nil)
        }
    }

}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51144606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档