Swift字典过滤

问题描述 投票:0回答:1

在几天尝试实现从远程JSON响应和搜索功能填充的表视图之后,我想我到了工作的最后。

到目前为止,这是我在SO的一些专家的帮助下所做的。

class DirectorioViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    var isSearching = false


    let URL_GET_DATA = "http://.../leer_directorio_todos.php"

    var directorios = [DirectorioCompleto]()
    var filteredData = [DirectorioCompleto]()


    override func viewDidLoad() {
        ....

        cargarDatos()
    }

    public func cargarDatos (){
        //fetching data from web api
        Alamofire.request(URL_GET_DATA).responseJSON { response in
            self.directorios.removeAll()

            //getting json
            if let json = response.result.value as? [[String:String]] {
                print (json)

                //traversing through all elements of the array
                for dict in json {

                    self.directorios.append(DirectorioCompleto(
                        nombre: dict["nombre"],
                        apellidos: dict["apellidos"],
                        apodo: dict["apodo"],
                        cumple: dict["cumple"],
                        conyuge: dict["conyuge"],
                        cumple_conyuge: dict["cumple_conyuge"],
                        aniversario_bodas: dict["aniversario_bodas"],
                        empresa: dict["empresa"],
                        direccion_empresa: dict["direccion_empresa"],
                        tel_negocio: dict["tel_negocio"],
                        fecha_ingreso: dict["fecha_ingreso"],
                        num_rotario: dict["num_rotario"],
                        padrino: dict["padrino"],
                        direccion_casa: dict["direccion_casa"],
                        tel_casa: dict["tel_casa"],
                        celular: dict["celular"],
                        email: dict["email"],
                        email_privado: dict["email_privado"],
                        clasificacion: dict["clasificacion"],
                        imagen: dict["imagen"]
                    ))

                }
            }

            //displaying data in tableview
            self.tableView.reloadData()
        }
    }

    //the method returning size of the list
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        if isSearching{
            return filteredData.count
        }
        return directorios.count
    }


    //the method returning each cell of the list
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DirectorioTableViewCell
        let directorio: DirectorioCompleto

        if isSearching {
           directorio = filteredData[indexPath.row]

        } else {
            directorio = directorios[indexPath.row]
        }

        // set the data...

        return cell
    }


    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if searchBar.text == nil || searchBar.text == "" {

            isSearching = false

            view.endEditing(true)

            tableView.reloadData()

        } else {

            isSearching = true

            filteredData = directorios.filter({$0 == searchBar.text!})

            tableView.reloadData()
        }
    }
}

现在我正在尝试在字典中实现搜索功能,在搜索Google和SO之后我需要你的帮助来完成最后一部分:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if searchBar.text == nil || searchBar.text == "" {

            isSearching = false

            view.endEditing(true)

            tableView.reloadData()

        } else {

            isSearching = true

            filteredData = directorios.filter({$0 == searchBar.text!})

            tableView.reloadData()
        }
    }

我该如何解决这最后一部分?如何过滤所有键或几个键的整个字典?

swift dictionary
1个回答
0
投票

我现在拥有它,谢谢大家的支持和建议:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {

        isSearching = false

        view.endEditing(true)

        tableView.reloadData()

    } else {

        isSearching = true

        filteredData = self.directorios.filter{($0.nombre?.localizedCaseInsensitiveContains(searchBar.text!))! || ($0.apellidos?.localizedCaseInsensitiveContains(searchBar.text!))!}

        tableView.reloadData()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.