获取二次数据以显示我的可调意见。 我有3个数据来自APIC(姓名、地址、电话)。 没有出现“Name”的问题,但当我试图显示次要数据(地址、电话)时,我被困。 我在此有两份法典文件,一份是“Controller”文件,正在处理表文和一份处理数据分类的示范文件。 二级数据在我的代码中称为“尾矿”。 任何帮助都受到高度赞赏。
第一部《法典》档案是Gitroller,第二部是示范档案。
import UIKit
struct SportsItem {
let facility: String
let address: String
let telephone: String
}
class SportsResultsViewController: UIViewController, UITableViewDataSource {
var sportsData: [SportsItem] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sportsData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
cell.textLabel?.text = sportsData[indexPath.row].facility
cell.detailTextLabel?.numberOfLines = 0
cell.detailTextLabel?.text = "Address: (sportsData[indexPath.row].address)
Phone: (sportsData[indexPath.row].telephone)"
cell.textLabel?.font = UIFont.systemFont(ofSize: 8)
cell.detailTextLabel?.font = UIFont.systemFont(ofSize: 8)
return cell
}
var jsonString: String?
override func viewDidLoad() {
super.viewDidLoad()
resultsTableView.dataSource = self
resultsTableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellIdentifier")
resultsTableView.cellLayoutMarginsFollowReadableWidth = true
resultsTableView.rowHeight = UITableView.automaticDimension
resultsTableView.estimatedRowHeight = 164.0
if let jsonString = jsonString {
if let data = jsonString.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let features = json["features"] as? [[String: Any]] {
var sportsData: [SportsItem] = []
for i in 0..<min(10, features.count) {
if let feature = features[i]["properties"] as? [String: Any]? ?? [:],
let address = feature["Address"] as? String,
let telephone = feature["Telephone"] as? String,
let facility = feature["Facility Name"] as? String {
let sportsItem = SportsItem(facility: facility, address: address, telephone: telephone)
sportsData.append(sportsItem)
}
}
self.sportsData = sportsData
if sportsData.isEmpty {
// Handle empty data case if needed
}
resultsTableView.reloadData()
}
}
}
@IBAction func findHotelsPressed(_ sender: UIButton) {
UIApplication.shared.open(URL(string: "https://www.hotels.com/de606379/hotels-hong-kong-hong-kong-sar/")!)
}
@IBOutlet weak var resultsTableView: UITableView!
}
第二部法典文件。
import UIKit
struct SportsManager {
let sportsURL = "https://geodata.gov.hk/gs/api/v1.0.0/geoDataQuery?q=%7Bv%3A%221%2E0%2E0%22%2Cid%3A%22d1a2e8e0-8636-41e9-b664-0e64b86860a2%22%2Clang%3A%22ALL%22%7D"
func getSportsData(completion: @escaping (String?) -> Void) {
guard let url = URL(string: sportsURL) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
print(error.localizedDescription)
completion(nil)
return
}
guard let data = data else {
completion(nil)
return
}
do {
let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
let jsonString = try JSONSerialization.data(withJSONObject: jsonData ?? "", options: .prettyPrinted)
let sportsData = String(data: jsonString, encoding: .utf8)
completion(sportsData)
} catch let error {
print(error.localizedDescription)
completion(nil)
}
}.resume()
}
}