Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS Swift 2.1中完成json请求后重新加载视图/数据_Ios_Swift_Alamofire - Fatal编程技术网

在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)
        }
    }
}