2016-04-18 10 views
0

Ich versuche eine Activity Indicator über eine Table View zu implementieren, aber es funktioniert nicht richtig. Es erscheint unter dem Table CellsAktivitätsanzeige über die Tabellenansicht

Hier ist mein Code und das Bild davon, wie es scheint:

import UIKit 

class SelecionaPaisTableViewController: UITableViewController, UISearchResultsUpdating { 

    //MARK: - Propriedades 
    var paises = [PaisCodigo]() 
    var paisesFiltrado = [PaisCodigo]() 

    var controladorDeBusca: UISearchController! 

    var container: UIView = UIView() 
    var loadingView: UIView = UIView() 
    var indicadorDeAtividade: UIActivityIndicatorView! 

    //MARK: - Métodos reescritos da View 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     //Dados dos países 
     //carregaDadosPaises() 

     //Carrega configuração do SearchController 
     configurarControladorDeBusca() 

     //Carrega indicador de atividade 
     configurarIndicadorDeAtividade() 
    } 

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

    // MARK: - Métodos reescritos da Table view data source 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if controladorDeBusca.active { 
      return paisesFiltrado.count 
     } else { 
      return paises.count 
     } 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("PaisCell", forIndexPath: indexPath) 

     let pais: PaisCodigo 

     if controladorDeBusca.active { 
      pais = paisesFiltrado[indexPath.row] 
     } else { 
      pais = paises[indexPath.row] 
     } 

     cell.textLabel?.text = pais.nome + " (+" + String(pais.codigo) + ")" 

     if pais.nome != pais.nomeIngles { 
      cell.detailTextLabel?.text = pais.nomeIngles 
     } else { 
      cell.detailTextLabel?.text = "" 
     } 

     return cell 
    } 

    //MARK: - Métodos do UISearchResultsUpdating 
    func updateSearchResultsForSearchController(searchController: UISearchController) { 
     //paisesFiltrado.removeAll(keepCapacity: false) 

     filtrarBusca(controladorDeBusca.searchBar.text!) 
    } 

    //MARK: - Métodos 
    func carregaDadosPaises() { 
     //Carrega dados dos países 
     let pais1 = PaisCodigo(nome: "Brasil", nomeIngles: "Brazil", codigo: 55) 
     let pais2 = PaisCodigo(nome: "United States", nomeIngles: "United States", codigo: 1) 
     let pais3 = PaisCodigo(nome: "Argentina", nomeIngles: "Argentina", codigo: 54) 
     let pais4 = PaisCodigo(nome: "Australia", nomeIngles: "Australia", codigo: 61) 

     paises += [pais1, pais2, pais3, pais4] 

     //paisesTableView.reloadData() 
    } 

    func configurarControladorDeBusca() { 
     //Configura Controlador de Busca 
     controladorDeBusca = UISearchController(searchResultsController: nil) 
     controladorDeBusca.searchResultsUpdater = self 
     controladorDeBusca.dimsBackgroundDuringPresentation = false 
     definesPresentationContext = true 

     //Configura a barra do Controlador de busca 
     controladorDeBusca.searchBar.placeholder = "Search country" 
     controladorDeBusca.searchBar.sizeToFit() 
     controladorDeBusca.searchBar.barTintColor = navigationController?.navigationBar.barTintColor 
     controladorDeBusca.searchBar.translucent = true 
     controladorDeBusca.searchBar.tintColor = UIColor.whiteColor() 

     //Adiciona a barra do Controlador de Busca a Table View 
     tableView.tableHeaderView = controladorDeBusca.searchBar 
    } 

    func configurarIndicadorDeAtividade() { 

     //tableView.hidden = true 

     container.frame = tableView.frame 
     container.center = tableView.center 
     container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.5) 

     loadingView.frame = CGRectMake(0, 0, 80, 80) 
     loadingView.center = tableView.center 
     loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7) 
     loadingView.clipsToBounds = true 
     loadingView.layer.cornerRadius = 10 

     //Configura Indicador de atividade 
     indicadorDeAtividade = UIActivityIndicatorView() 
     indicadorDeAtividade.center = view.center 
     indicadorDeAtividade.hidesWhenStopped = true 
     indicadorDeAtividade.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge 

     //Adiciona o indicador a View 
     loadingView.addSubview(indicadorDeAtividade) 
     container.addSubview(loadingView) 
     tableView.addSubview(container) 

     //Inicia o indicador 
     tableView.userInteractionEnabled = false 
     indicadorDeAtividade.startAnimating() 
    } 

    func filtrarBusca(textoDeBusca: String) { 
     //Filtrar resultados de busca 
     paisesFiltrado = paises.filter{ PaisCodigo in 
      //if PaisCodigo.nome.lowercaseString.containsString(textoDeBusca.lowercaseString) || PaisCodigo.nomeIngles.lowercaseString.containsString(textoDeBusca.lowercaseString) { 

      //} 
      return PaisCodigo.nome.lowercaseString.containsString(textoDeBusca.lowercaseString) || PaisCodigo.nomeIngles.lowercaseString.containsString(textoDeBusca.lowercaseString) 
     } 

     tableView.reloadData() 
    } 

    func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor { 
     //Conversor de cores em HEX 
     let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0 
     let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0 
     let blue = CGFloat(rgbValue & 0xFF)/256.0 
     return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha)) 
    } 
} 

enter image description here

+0

haben Sie versuchen, den Blick auf die Super-Ansicht Hinzufügen: Mit dieser tableView.addSubview ändern (Container) für self.view.superview.addSubview (Container) – dminones

+0

ich das versucht, aber wenn ich auch, dass die graue Quadrat doesn nicht erscheinen. Es erscheint nur ein leerer Tisch. – GuiDupas

+0

Damit 'self.view.superview.addSubview (container)' funktionieren kann, müssen Sie den Aufruf von 'configurarIndicadorDeAtividade' wahrscheinlich in' viewWillAppear' oder 'viewDidAppear' verschieben, wie in' viewDidLoad', in dem die Ansicht noch nicht enthalten ist die Hierarchie und daher noch keine Superview. –

Antwort

0

Es funktionierte, wenn die Activity indicator Konfiguration innerhalb des viewDidAppear und statt tableView.addSubview(container) ich view.addSubview(container) verwendet gemacht wurde. Danke euch allen für die Hilfe.

override func viewDidAppear(animated: Bool) { 
     configurarIndicadorDeAtividade() 
    } 
0

ich glaube, Sie so etwas wie dieses verwenden sollte:

super.view.insertSubview(container, aboveSubview: tableView) 

Don 't vergessen zu machen container.userInteractionEnabled = false

Hoffe, es hilft :)

+0

Ja, aber es muss innerhalb des 'viewDidAppear' erfolgen – GuiDupas

Verwandte Themen