我在从托管的JSON文件填充我的tableview时遇到问题。我已经确认应用程序已成功查看JSON文件中的数据,但表本身仍为空白(奇怪的是,显示某些行的两个不同的垂直高度)。
这是我的ViewController.swift:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
// var heroes = [HeroStats]()
var bonuses = [JsonFile.JsonBonuses]()
override func viewDidLoad() {
super.viewDidLoad()
downloadJSON {
self.tableView.reloadData()
}
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("Found \(bonuses.count) rows in section.")
return bonuses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = bonuses[indexPath.row].name.capitalized
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? HeroViewController {
destination.bonus = bonuses[(tableView.indexPathForSelectedRow?.row)!]
}
}
// MARK: - Download JSON from ToH webserver
func downloadJSON(completed: @escaping () -> ()) {
let url = URL(string: "http://tourofhonor.com/BonusData.json")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
let posts = try JSONDecoder().decode(JsonFile.self, from: data!)
DispatchQueue.main.async {
completed()
}
print(posts.bonuses.map {$0.bonusCode})
} catch {
print("JSON Download Failed")
}
}
}.resume()
}
}
以下是JsonFile.swift文件的样子:
import Foundation
struct JsonFile: Codable {
struct Meta: Codable {
let fileName: String
let version: String
}
struct JsonBonuses: Codable {
let bonusCode: String
let category: String
let name: String
let value: Int
let city: String
let state: String
let flavor: String
let imageName: String
}
let meta: Meta
let bonuses: [JsonBonuses]
}
tableView numberOfSections中的那个打印显示0,我注意到我看到打印了三次,然后我看到代码的打印表明JSON已被读取,然后我看到“找到0行的部分”打印再次。
我在这里缺少什么?
答案 0 :(得分:1)
在数据源方法中,您正在从import scrapy
class PvSpider(scrapy.Spider):
name = 'pv'
allowed_domains = ['www.piaov.com']
start_urls = ['http://www.piaov.com/']
def start_requests(self):
yield scrapy.Request(url='http://www.piaov.com/list/7.html')
def parse(self, response):
for name in response.xpath("//ul[@class='mlist']//li/a/@title").extract():
yield {"name": name}
for p in range(2, 7):
yield scrapy.Request(url='http://www.piaov.com/list/7_{}.html'.format(p),
callback=self.parse)
数组中读取数据。但是,当您完成下载帖子后,您不会将帖子的奖金分配给bonuses
数组。
bonuses