代码之家  ›  专栏  ›  技术社区  ›  forrest

如何使用webkit在html中显示更多json项?

  •  1
  • forrest  · 技术社区  · 7 年前

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

    以下是feed元素:

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

    我将数据发送到详细视图控制器,如下所示:

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

    然后在详细视图控制器上显示代码。评论的项目是我要添加到显示中的项目:

    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)
            }
        }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Christian Abella    7 年前

    您只需要正确地转义html字符串,您的数据就会显示出来。

    if let description = articleSummary["description"],
       let title = articleSummary["title"],
       let author = articleSummary["author"],
       let image = articleSummary["image"],
       let date = articleSummary["date"] {
            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)
    }