我是新手,问题是这是一个职业数据库,想根据表中所选职业加载类,并使用代码中显示的链接,但是如果连接不良,在完成函数后,正在加载数组“ Courses”,为此,我想添加一个句子以使函数在未加载数组时保持运行,但是我意识到活动指示器应该在加载数组时出现,实际上出现了在函数的末尾,尽管我在函数的开头调用它,但是我的问题是,自从将职业压在桌子上以来,如何使活动指示符一直存在,直到“课程”数组加载了职业的所有过程。请帮助我:(!这是代码...

 [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,因此您需要将它们分派到主队列上,因为完成关闭将在另一个线程上执行。

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)

    }
}

07-24 09:25