搜索JSON响应以重新加载tableView

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

继续我之前的问题Trying to compare Any? with String我想知道我应该实现什么来重新加载tableview,其中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()
        }
    }
swift
1个回答
1
投票

要搜索您的目录,请执行此操作。我假设您必须按键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()

我希望这能帮到您。

© www.soinside.com 2019 - 2024. All rights reserved.