gpt4 book ai didi

ios - 在 iOS Swift 2.1 中完成 json 请求后重新加载 View /数据

转载 作者:搜寻专家 更新时间:2023-11-01 06:20:08 25 4
gpt4 key购买 nike

我有一个 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 请求完成后重新加载 View :

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
}
...

目前,我只能看到带有“动物”、“车辆”和“电影” TableView 部分的静态数据,没有“事件”部分。

我相信我需要在某处重新加载 View 。我该怎么做?

最佳答案

首先,在您的 getSectionsFromData() 中,您正在调用对 Alamofire.request() 的异步调用,它不会在 的适当时间执行code>sectionsArray 以获取返回的数据。您实际上是从 getSectionsFromData 返回静态数组。

要解决此问题,您可以在 getSectionsFromData()

中使用回调
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)
}
}
}

Swift Closures

关于ios - 在 iOS Swift 2.1 中完成 json 请求后重新加载 View /数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35309353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com