2017-08-15 3 views
0

In meinem Xcode-Projekt lade ich Objekte in ein Array und präsentiere es dann auf einem UITableViewController. Während diese Artikel laden, ist die UITableViewController leer und ist nicht ästhetisch ansprechend, also möchte ich einen Aktivitätsindikator in die UITableViewController setzen, während er die Daten lädt. Ich habe meinen Code unten kopiert. Wie implementiere ich das? (ich hörte etwas ein Abschluss-Handler möglicherweise genannt den Trick tun könnte, aber ich bin nicht sicher, wie es zu benutzen)Aktivitätsanzeige, während Tabellenansicht geladen wird

func loadAnimals() { 
    let animalQuery = PFQuery(className: "Animals") 
    animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user 
    animalQuery.limit = 10 
    animalQuery.findObjectsInBackground { (objects, error) in 
     if error == nil { 
      self.colorArray.removeAll(keepingCapacity: false) 
      self.animalNameArray.removeAll(keepingCapacity: false)     
      self.animalTypeArray.removeAll(keepingCapacity: false) 

      for object in objects! { 
       self.colorArray.append(object.value(forKey: "colorType") as! String) // add data to arrays 
       self.animalNameArray.append(object.value(forKey: "animalName") as! String) // add data to arrays      
       self.animalTypeArray.append(object.value(forKey: "animalType") as! String) // add data to arrays 
      } 
      self.tableView.reloadData() 

     } else { 
      print(error?.localizedDescription ?? String()) 
     } 
    } 
} 

// legt Farben in Reihen

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! AnimalCell //connects to color cell 

    //Adds the animal information in the cells 
    cell.colorType.text = colorArray[indexPath.row] 
    cell.animalName.text = animalNameArray[indexPath.row] 
    cell.animalType.text = animalTypeArray[indexPath.row] 

    return cell 
} 
+0

Mögliche Duplikat [Ort-Aktivitätsanzeige über uitable Ansicht] (https://stackoverflow.com/questions/29311093/place-activity-indicator-over-uitable- view) – MwcsMac

Antwort

1

Es scheint, Sie haben eine asynchrone Funktion zum Laden der Tabellensichtdaten. Sie müssen nur dort activityIndicator.stopAnimating() setzen.

Fügen Sie einen UIActivityIndicatorView entweder programmatisch oder über Storyboard zu Ihrem Tabellenansicht-Controller hinzu. Wenn Sie es in Code zu tun, das ist, wie Sie es tun, einschließlich Einschränkungen Hinzufügen es Ihrer Ansicht-Controller in der Mitte zu halten:

view.addSubview(activityIndicator) 
activityIndicator.translatesAutoresizingMaskIntoConstraints = false 
activityIndicator.hidesWhenStopped = true 
activityIndicator.color = UIColor.black 
let horizontalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0) 
view.addConstraint(horizontalConstraint) 
let verticalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0) 
view.addConstraint(verticalConstraint) 

Bevor Sie den obigen Code innerhalb viewDidLoad() Aufruf erklären let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) irgendwo in deine Klasse.

Sie müssen nur beginnen, es zu animieren, sobald loadAnimals die Ausführung startet und vor dem Aufruf tableView.reloadData() stoppen. Nennen Sie es `activityIndicator, dann gehen Sie wie folgt vor:

func loadAnimals() { 
    activityIndicator.startAnimating() 
    let animalQuery = PFQuery(className: "Animals") 
    animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user 
    animalQuery.limit = 10 
    animalQuery.findObjectsInBackground { (objects, error) in 
     if error == nil { 
      self.colorArray.removeAll(keepingCapacity: false) 
      self.animalNameArray.removeAll(keepingCapacity: false)     
      self.animalTypeArray.removeAll(keepingCapacity: false) 

      for object in objects! { 
       self.colorArray.append(object.value(forKey: "colorType") as! String) // add data to arrays 
       self.animalNameArray.append(object.value(forKey: "animalName") as! String) // add data to arrays      
       self.animalTypeArray.append(object.value(forKey: "animalType") as! String) // add data to arrays 
      } 
      activityIndicator.stopAnimating() 
      self.tableView.reloadData() 

     } else { 
      print(error?.localizedDescription ?? String()) 
     } 
    } 
} 
+1

Großartig funktioniert das perfekt! Vielen Dank – fphelp

0
//SOMEWHERE ELSE IN YOUR CODE  
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) 
    activityIndicator.hidesWhenStopped = true 
    self.view.addSubview(activityIndicator) 

///.... where you load animals 
func loadAnimals { 
/// 
    activityIndicator.startAnimating() 
    ////your code 
    ///... 
    animalQuery.findObjectsInBackground { (objects, error) in 
     if error == nil { 
     ///Stop the spinner 
     activityIndicator.stopAnimating() 
     ///...rest of your code 
     } 
     } 
Verwandte Themen