gpt4 book ai didi

ios - 当使用 JSON 在后台加载数组并在 TableView 中进行选举时,事件指示器不会消失

转载 作者:行者123 更新时间:2023-11-30 13:15:34 24 4
gpt4 key购买 nike

我是 swift 的新手,问题是这个,是一个职业数据库,想要根据表中所选的职业加载类,这与代码中出现的链接有关,但万一连接不良,数组“Courses”在完成函数后加载,为此我想添加一句话以在数组未加载时保持函数运行,但我意识到事件指示器应该在数组加载时出现,实际上出现在函数的末尾,尽管我在函数的开头调用它,那么我的问题是如何使事件指示器出现,因为职业压在 table 上,直到“类(class)”数组加载职业的所有类(class)。请帮助我:(!这是代码...

 [import UIKit
class CarreraTableViewController: UIViewController {

//Var to InicioApp ViewController
var CarreraSeleccionada = String()

//OK BarButton
@IBOutlet weak var Listo: UIBarButtonItem!

//Courses array
var Cursos = \[String\]()

//The Activity Indicator is in a UIView in the storyboard to more easily customize
@IBOutlet weak var ActivityIndicatorView: UIActivityIndicatorView!
@IBOutlet weak var ActivityContainer: UIView!

//Careers Array
var Carreras = \["Enlace Escolar - Antofagasta", "Enlace Escolar - Coquimbo", "Geología", "Ingeniería Civil Ambiental", "ICCI – Antofagasta", "ICCI – Coquimbo", "Ingeniería Civil", "Ingeniería Civil Industrial – Antofagasta", "Ingeniería Civil Industrial – Coquimbo", "Ingeniería Civil Metalúrgica", "Ingeniería Civil de Minas", "Ingeniería Civil Química", "Ingeniería en Construcción", "Ingeniería en Computación e Informática", "Ingeniería en Metalurgia", "Ingeniería en Procesos Químicos", "IPRYMA - Antofagasta", "IPRYMA - Coquimbo"\]

override func viewDidLoad() {
super.viewDidLoad()

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Carreras.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "MyCell"
let showData = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomCellCarrera
let Carrera = Carreras\[indexPath.row\]
showData.CarreraLabel.text = Carrera

return showData

}
// Identifies which table row the user selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {




// ================= At this point I have my question, because ActivityContainer and ActivityIndicatorView does not appear until the end function

Listo.enabled = false
ActivityContainer.hidden = false
ActivityIndicatorView.startAnimating()

let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as! CustomCellCarrera
print(currentCell.CarreraLabel.text)

var arregloArea = \[String\]()
var arregloID = \[Int\]()

Cursos.removeAll()

var i = -1

if let CR = currentCell.CarreraLabel.text {
switch CR {
case "ICCI – Coquimbo":
i = 1
case "Ingeniería Civil Industrial – Coquimbo":
i = 2
case "ICCI – Antofagasta":
i = 10
case "Enlace Escolar - Antofagasta":
i = 15
case "Enlace Escolar - Coquimbo":
i = 14
case "Geología":
i = 16
case "Ingeniería Civil Ambiental":
i = 12
case "Ingeniería Civil":
i = 5
case "Ingeniería Civil Industrial – Antofagasta":
i = 4
case "Ingeniería Civil Metalúrgica":
i = 7
case "Ingeniería Civil de Minas":
i = 11
case "Ingeniería Civil Química":
i = 6
case "Ingeniería en Construcción":
i = 3
case "Ingeniería en Computación e Informática":
i = 13
case "Ingeniería en Metalurgia":
i = 17
case "Ingeniería en Procesos Químicos":
i = 18
case "IPRYMA - Antofagasta":
i = 8
case "IPRYMA - Coquimbo":
i = 9
default:
i = -1
}

}

//Varia i según carrera
let url = NSURL(string: "http://146.83.128.64/tongoy/g.php?&sala=-1&curso=-1&profesor=-1&semestre=-1&semestrec=-1&carrera=\(i)&area=-1")!
print(url)
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in

if let urlContent = data {

do {

let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)

for json in jsonResult as! Array<AnyObject> {

if let area = json\["area"\] as? String {
arregloArea.append(area)
} else {

}
if let id = json\["id"\] as? Int {
arregloID.append(id)
}else{

}
if let curso = json\["curso"\] as? String {
self.Cursos.append(curso)
}else{

}

}

} catch {
print("JSON serialization failed")
}

dump(self.Cursos)

}
}
task.resume()

CarreraSeleccionada = currentCell.CarreraLabel.text!

while Cursos.isEmpty{

}
if !Cursos.isEmpty{
Listo.enabled = true
ActivityIndicatorView.stopAnimating()
ActivityContainer.hidden = true

}
}
}

//To InicioApp VC

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
let DestViewController : InicioApp = segue.destinationViewController as! InicioApp

//Llamado a funcion de eliminar ramos duplicados
var RepCurso = removeDuplicates(Cursos)
RepCurso.sortInPlace(before)

print(RepCurso)

DestViewController.CursosCarrera = RepCurso
DestViewController.TableText = CarreraSeleccionada

}

func before(value1: String, value2: String) -> Bool {
// One string is alphabetically first.
// ... True means value1 precedes value2.
return value1 < value2;
}

func removeDuplicates(array: \[String\]) -> \[String\] {
var encountered = Set<String>()
var result: \[String\] = \[\]
for value in array {
if encountered.contains(value) {
// Do not add a duplicate element.
}
else {
// Add value to the set.
encountered.insert(value)
// ... Append the value.
result.append(value)
}
}
return result
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}][1]

最佳答案

您的循环 while Cursos.isEmpty{ 将阻塞主线程,从而阻止对 UI 的任何更新。

您需要在完成闭包中执行所需的任务,而不是在函数末尾。另外,由于您正在更新 UI,因此需要将它们分派(dispatch)到主队列上,因为完成闭包将在另一个线程上执行。

let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in

if let urlContent = data {

do {

let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)

for json in jsonResult as! Array<AnyObject> {

if let area = json\["area"\] as? String {
arregloArea.append(area)
} else {

}
if let id = json\["id"\] as? Int {
arregloID.append(id)
}else{

}
if let curso = json\["curso"\] as? String {
self.Cursos.append(curso)
}else{

}

}

} catch {
print("JSON serialization failed")
}
dispatch_async(dispatch_get_main_queue(), ^{
Listo.enabled = true
ActivityIndicatorView.stopAnimating()
ActivityContainer.hidden = true
});

dump(self.Cursos)

}
}

关于ios - 当使用 JSON 在后台加载数组并在 TableView 中进行选举时,事件指示器不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276747/

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