2015-03-27 12 views
20

Umieściłem wskaźnik aktywności w kodzie UITableView w serii ujęć. Jak mogę wyśrodkować je na środku ekranu i przed widokiem tabeli?Wskaźnik aktywności miejsca nad dostępnym widokiem

Próbowałem:

self.activityIndicator.center = self.tableView.center 

Ale teraz, wskaźnik aktywności jest w górnej połowie widok!

Odpowiedz

16

Oto kroki: Postępuj zgodnie z instrukcjami Chrissukhram.

  1. przeciągania widok od góry widok tabeli kroku
  2. Aktywność formierska wskaźnik do widoku
  3. wyrównać wskaźnik aktywności do środku poziomej i pionowej, w pojemniku

  4. wprowadzania IBOutlet połączenie dla wskaźnika widoku i działania

  5. Rozpocznij aktywację Ty wskaźnik
  6. przystanek i hide: activityIndicatorLarge.hidesWhenStopped = true activityView.hidden = true
3

Jeśli wskaźnik aktywności jest dla informacji załadowaniem do UITableView należy utworzyć osobne ładowanie UIView który jest taki sam rozmiar jak UITableView i umieść wskaźnik aktywności w centrum tego widoku, a nie sam UITableView.

Następnie dodaj załadunek UIView do głównego widoku (nad widokiem tabeli) i ustaw go jako ukryty lub widoczny w zależności od stanu załadowania.

+1

jak mogę umieścić UIView nad Tableview? –

+0

Możesz dodać ładowanie UIView do głównego widoku ViewControllers (self.view) po dodaniu swojego UITableView. Zasadniczo kolejność dodawania widoków do siebie to kolejność, w jakiej będą one układane w stosy, gdy są wyświetlane. – chrissukhram

+0

Muszę więc zmienić scenorys w ten sposób: –

28

Aby wyświetlić wskaźnik aktywności, podczas gdy dane są ładowane w UITableView, można utworzyć UIView i dodać go do widoku głównego (w środku UITableView). Jeśli chcesz je wyświetlić, jak pokazano poniżej:

enter image description here

Można to zrobić z fnuctions setLoadingScreen i removeLoadingScreen (jeśli chcesz zobaczyć przykładowy projekt można przejść here):

class TableViewController: UITableViewController { 

    // MARK: Properties 

    /// Data source of the tableView 
    var rows: [String] = [] 

    /// View which contains the loading text and the spinner 
    let loadingView = UIView() 

    /// Spinner shown during load the TableView 
    let spinner = UIActivityIndicatorView() 

    /// Text shown during load the TableView 
    let loadingLabel = UILabel() 


    // MARK: Methods 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     setLoadingScreen() 

     loadData() 

    } 

    override func didReceiveMemoryWarning() { 

     super.didReceiveMemoryWarning() 

    } 


    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 

     return 1 

    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return rows.count 

    } 

    override func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)! 
     cell.textLabel!.text = rows[indexPath.row] 

     return cell 

    } 


    // MARK: Private methods 

    // Load data in the tableView 
    private func loadData() { 

     // Simulate a delay of some operations as a HTTP Request    
     DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(20)) { 

      self.rows = ["Row 1", "Row 2", "Row 3", "Row 4", "Row 5"] 

      self.tableView.reloadData() 
      self.tableView.separatorStyle = .singleLine 
      self.removeLoadingScreen() 

     } 

    } 

    // Set the activity indicator into the main view 
    private func setLoadingScreen() { 

     // Sets the view which contains the loading text and the spinner 
     let width: CGFloat = 120 
     let height: CGFloat = 30 
     let x = (tableView.frame.width/2) - (width/2) 
     let y = (tableView.frame.height/2) - (height/2) - (navigationController?.navigationBar.frame.height)! 
     loadingView.frame = CGRect(x: x, y: y, width: width, height: height) 

     // Sets loading text 
     loadingLabel.textColor = .gray 
     loadingLabel.textAlignment = .center 
     loadingLabel.text = "Loading..." 
     loadingLabel.frame = CGRect(x: 0, y: 0, width: 140, height: 30) 

     // Sets spinner 
     spinner.activityIndicatorViewStyle = .gray 
     spinner.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
     spinner.startAnimating() 

     // Adds text and spinner to the view 
     loadingView.addSubview(spinner) 
     loadingView.addSubview(loadingLabel) 

     tableView.addSubview(loadingView) 

    } 

    // Remove the activity indicator from the main view 
    private func removeLoadingScreen() { 

     // Hides and stops the text and the spinner 
     spinner.stopAnimating() 
     spinner.isHidden = true 
     loadingLabel.isHidden = true 

    } 

} 
+1

Ładne rozwiązanie ... – Dasoga

+1

+1, ale w moim przypadku removeLoadingScreen() nie zachowywał się poprawnie, etykieta i wskaźnik były nadal widoczne nawet po pojawieniu się komórek tabeli. Po kilku sekundach etykieta i wskaźnik zniknęły. Naprawiłem to za pomocą self.loadingView.removeFromSuperview() w removeLoadingScreen() – Jurasic

2

Myślę, że użyłeś bezpośrednio UITableView w twoim storybooku. Dlatego nie można przeciągnąć uiview do głównego widoku (tableview). Usuń kontroler UItableview i zamiast tego przeciągnij obiekt UIViewController do serii ujęć. Następnie przeciągnij do niego widok stołowy. następnie przeciągnij inny widok ponad widok tabeli dla wskaźnika aktywności. nie zapomnij o obsłudze metod UItableviewdelegate i datasourse w twoim UIViewController.

3

Jeśli używasz złożony widok:

func setupSpinner(){ 
    spinner = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height:40)) 
    spinner.color = UIColor(Colors.Accent) 
    self.spinner.center = CGPoint(x:UIScreen.main.bounds.size.width/2, y:UIScreen.main.bounds.size.height/2) 
    self.view.addSubview(spinner) 
    spinner.hidesWhenStopped = true 
} 
Powiązane problemy