【发布时间】:2016-05-27 02:05:26
【问题描述】:
我正在用 JSON 数据填充我的 tableView,大多数时候数据会显示,但由于某些奇怪的原因,有时它不会。我在 Chrome 中测试了 JSON 数据,信息就在那里。我还做了打印声明以在下载信息后打印信息,并且似乎下载正确。我无法弄清楚为什么 80% 的数据正确填充 tableView 而 20% 的时间却没有。这是我的代码示例,还有更多单元格,但在此示例中我将其缩短为 2:
var task : NSURLSessionTask?
var newURL : String?
var bannerArray: [String] = []
var overViewArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
getJSON(newURL!)
}
func getJSON (urlString: String) {
let url = NSURL(string: urlString)!
let session = NSURLSession.sharedSession()
task = session.dataTaskWithURL(url) {(data, response, error) in
dispatch_async(dispatch_get_main_queue()) {
if (error == nil) {
self.updateDetailShowInfo(data)
}
else {
"Not getting JSON"
}
}
}
task!.resume()
}
func updateDetailShowInfo (data: NSData!) {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
guard let banner = jsonResult["banner"] as? String,
let overview = jsonResult["overview"] as? String
else { return }
_ = ""
print(overview)
bannerArray.append(banner)
overViewArray.append(overview)
}
catch {
print("It ain't working")
}
self.DetailTvTableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: return bannerArray.count
case 1: return overViewArray.count
default: fatalError("Unknown Selection")
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("bannerCell", forIndexPath: indexPath) as! BannerCell
cell.bannerImage.sd_setImageWithURL(NSURL(string: bannerArray[indexPath.row]))
self.DetailTvTableView.rowHeight = 100
DetailTvTableView.allowsSelection = false
return cell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("overviewCell", forIndexPath: indexPath) as! OverviewCell
let overViewText = overViewArray[indexPath.row]
if overViewText != "" {
cell.overView.text = overViewText
} else {
cell.overView.text = "N/A"
}
self.DetailTvTableView.rowHeight = 200
print(overViewArray[indexPath.row])
return cell
default: ""
}
return cell
}
【问题讨论】:
-
你能定义“不工作”吗?该代码的哪些部分在运行时运行或不运行?
-
tableView 似乎没有呈现 JSON 数据。它根本不显示任何单元格。奇怪的是,它只是有时会这样做,大部分时间都有效。
-
所以它仍在加载 JSON,只是没有显示它?也就是说,
updateDetailShowInfo仍然被调用并正常工作? -
是的,情况似乎如此,尽管由于某些奇怪的原因 self.DetailTvTableView.reloadData() 并不总是被调用。
-
有几个代码路径不会调用 reloadData。例如,您的
guard可能失败或 JSON 错误不为零。你有没有通过代码来查看它的发展方向?
标签: ios json swift uitableview tableview