如何使用 WebKit 在 HTML 中显示更多 JSON 项?
How do I display more JSON items in HTML using WebKit?
我正在使用 SwiftyJSON 构建一个新闻应用程序,并且我已经能够正确提取数据。我还能够在 tableView 中显示标题和描述。但是,当我转到详细信息视图时,我希望能够显示提要中的完整文章摘要。
这是提要元素:
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)
}
}
}
您只需正确转义 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)
}
我正在使用 SwiftyJSON 构建一个新闻应用程序,并且我已经能够正确提取数据。我还能够在 tableView 中显示标题和描述。但是,当我转到详细信息视图时,我希望能够显示提要中的完整文章摘要。
这是提要元素:
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)
}
}
}
您只需正确转义 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)
}