在iOS Swift 2.1中完成json请求后重新加载视图/数据
我有一个在iOS Swift 2.1中完成json请求后重新加载视图/数据,ios,swift,alamofire,Ios,Swift,Alamofire,我有一个SectionsData.swift文件,在其中我从get http请求中获取一些数据: import Foundation import Alamofire class SectionsData { func getSectionsFromData() -> [Section] { var sectionsArray = [Section]() let animals = Section(title: "Animals", object
SectionsData.swift
文件,在其中我从get http请求中获取一些数据:
import Foundation
import Alamofire
class SectionsData {
func getSectionsFromData() -> [Section] {
var sectionsArray = [Section]()
let animals = Section(title: "Animals", objects: ["Cats", "Dogs", "Lions", "Birds"], place: "Jungle")
let vehicles = Section(title: "Vehicles", objects: ["Cars", "Bicycle"], place: "Road")
let movies = Section(title: "Movies", objects: ["Sound", "Music"], place: "Cinema")
sectionsArray.append(animals)
sectionsArray.append(vehicles)
sectionsArray.append(movies)
Alamofire.request(.GET, "https://example.com")
.responseJSON { response in
if let JSON = response.result.value {
var openEvents = Section(title: "Events", objects: [], place: "Rooms")
for index in 0...9 {
let eventName = JSON["events"]!![index]["name"]! as! String
openEvents.items.append(eventName)
}
sectionsArray.append(openEvents)
}
}
return sectionsArray
}
}
在ViewController.swift
文件中,我试图在JSON请求完成后重新加载视图:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var sections: [Section] = SectionsData().getSectionsFromData()
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].heading
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].items.count
}
...
目前,我只看到“动物”、“车辆”和“电影”的表视图部分的静态数据,没有“事件”部分
我想我需要在某个地方重新加载视图。如何执行此操作?首先,在
getSectionsFromData()
中,您正在异步调用Alamofire.request()
,该调用将不会在sectionsArray
获得返回数据的适当时间内执行。实际上,您正在从getSectionsFromData
返回静态数组
要解决此问题,可以在getSectionsFromData()中使用回调
首先,在getSectionsFromData()
中,您正在调用对Alamofire.request()
的异步调用,该调用将不会在sectionsArray
获得返回数据的适当时间内执行。实际上,您正在从getSectionsFromData
返回静态数组
要解决此问题,可以在getSectionsFromData()中使用回调
如果只是更新TableView中的数据,请尝试self.TableView.reloadData()如果只是更新TableView中的数据,请尝试self.TableView.reloadData()
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var sections: [Section] = []
override func viewDidLoad() {
super.viewDidLoad()
SecionsData().getSectionsFromData({
sections in
self.sections = sections
self.tableView.reloadData()
})
}
}
class SectionsData {
func getSectionsFromData(callback: (sections: [Section]) -> ()) {
var sectionsArray = [Section]()
let animals = Section(title: "Animals", objects: ["Cats", "Dogs", "Lions", "Birds"], place: "Jungle")
let vehicles = Section(title: "Vehicles", objects: ["Cars", "Bicycle"], place: "Road")
let movies = Section(title: "Movies", objects: ["Sound", "Music"], place: "Cinema")
sectionsArray.append(animals)
sectionsArray.append(vehicles)
sectionsArray.append(movies)
Alamofire.request(.Get, "https://example.com")
.responseJSON { response in
if let JSON = response.result.value {
var openEvents = Section(title: "Events", objects: [], place: "Rooms")
for index in 0...9 {
let eventName = JSON["events"]!![index]["name"]! as! String
openEvents.items.append(eventName)
}
sectionsArray.append(openEvents)
}
// This will pass the sections with or without any fetched data into the callback
callback(sections: sectionsArray)
}
}
}