Swift - Activity index on Table view

Asked

Viewed 90 times

0

I’m trying to implement a Activity Indicator about a Table View but it’s not working properly. It appears underneath the Table Cells

Here is my code and the image of how the indicator appears.

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

1 answer

2


Load the activity indicator in viewDidAppear() and not viewDidLoad()

  • It worked. Thank you Ccastro.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.