搜索JSON响应对重新加载tableView



作为我以前的问题的延续,试图比较任何问题?用字符串我想知道我应该使用符合Textfield输入的搜索条件的JSON响应对象重新加载表视图。

这是当前功能:

public func cargarDatos_filtrados (){
        //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 {
                    let nom = dict["nombre"]
                    if (nom == self.texto_buscado.text){
                       //DO SOMETHING
                    }

                    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()
        }
    }

用于搜索您的目录,请实现此目标。我假设您必须通过键nombre及其字符串搜索。

将此代码放在哪里进行搜索。

 if let text = self.texto_buscado.text
            {
                let filteredArray = self.directorios.filter{$0.nombre.localizedCaseInsensitiveContains(text) || $0.apellidos.localizedCaseInsensitiveContains(text)}

或确切匹配

                let filteredArray = self.directorios.filter{$0.nombre == text || $0.apellidos == text}
            }

filteredArray作为搜索结果,并使用此数组重新加载 tableView

self.tableView.reloadData()

我希望这对您有帮助。

最新更新