从不同的JSON响应中填充可扩展的TableView

时间:2017-12-05 02:43:57

标签: ios json uitableview alamofire

我正在开发一款iOS应用,其中包含一个带有表格视图的视图控制器。表格视图有4个可扩展部分。

这是当前的代码:

import UIKit
import Alamofire


class AniversariosViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate {

    @IBOutlet weak var tableView: UITableView!
     var arrRes = [[String:AnyObject]]() //Array of dictionar

    let URL_GET_CUMPLEANOS:String = "http://.../cumpleanos_ios.php"

    var sections = [
        Section(genre: "CUMPLEAÑOS DE ROTARIOS",
                aniversarios: ["The Lion King", "The Incredibles"],
                expanded: false),
        Section(genre: "CUMPLEAÑOS DE CONYUGES",
                aniversarios: ["Guardians of the Galaxy", "The Flash", "The Avengers", "The Dark Knight"],
                expanded: false),
        Section(genre: "ANIVERSARIOS DE BODAS",
                aniversarios: ["The Walking Dead", "Insidious", "Conjuring"],
                expanded: false),
        Section(genre: "ANIVERSARIOS DE INGRESOS EN CLUB ROTARIO",
                aniversarios: ["The Walking Dead", "Insidious", "Conjuring"],
                expanded: false)
    ]
    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(URL_GET_CUMPLEANOS).responseJSON { response in
            switch response.result {
            case .success:
                print(response)



            case .failure(let error):
                 print(error)
            }
        }


    }



    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].aniversarios.count
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (sections[indexPath.section].expanded) {
            return 44
        } else {
            return 0
        }
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 2
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = ExpandableHeaderView()
        header.customInit(title: sections[section].genre, section: section, delegate: self as! ExpandableHeaderViewDelegate)
        return header
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
        cell.textLabel?.text = sections[indexPath.section].aniversarios[indexPath.row]
        return cell
    }
    func toggleSection(header: ExpandableHeaderView, section: Int) {
        sections[section].expanded = !sections[section].expanded


        tableView.beginUpdates()
        for i in 0 ..< sections[section].aniversarios.count {
            tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
        }
        tableView.endUpdates()
    }


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


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

此时,这四个部分是从var部分填充的。我的下一步是通过Alamofire从JSON响应中填充每个部分。

到目前为止我所拥有的是具有此结构和内容的JSON响应:

(
        {
        "aniversario_bodas" = "0000-00-00";
        apellidos = xxx;
        apodo = "xxx";
        celular = "";
        clasificacion = "";
        conyuge = "";
        cumple = "0000-00-00";
        "cumple_conyuge" = "0000-00-00";
        "direccion_casa" = "";
        "direccion_empresa" = "";
        email = "xx";
        "email_privado" = "xxx";
        empresa = "";
        "fecha_ingreso" = "0000-00-00";
        "id_directorio" = 90;
        imagen = "eqzajAIan.jpg";
        nombre = " ";
        "num_rotario" = "";
        padrino = "";
        "tel_casa" = "";
        "tel_negocio" = "";
        "usuario_app" = 0;
    },
        {
        "aniversario_bodas" = "0000-00-00";
        apellidos = "xx";
        apodo = "xx";
        celular = "xxx";
        clasificacion = "";
        conyuge = "";
        cumple = "0000-00-00";
        "cumple_conyuge" = "0000-00-00";
        "direccion_casa" = "";
        "direccion_empresa" = "";
        email = "rxxx";
        "email_privado" = "xx";
        empresa = "";
        "fecha_ingreso" = "0000-00-00";
        "id_directorio" = 88;
        imagen = "qOCwITgobernador.jpg";
        nombre = " ";
        "num_rotario" = "";
        padrino = "";
        "tel_casa" = "";
        "tel_negocio" = "";
        "usuario_app" = 0;
    },
        {
        "aniversario_bodas" = "xxx";
        apellidos = "xx";
        apodo = ;
        celular = "xx";
        clasificacion = Medico;
        conyuge = "xxx";
        cumple = "0000-00-00";
        "cumple_conyuge" = "1980-11-22";
        "direccion_casa" = "xx";
        "direccion_empresa" = "xxx";
        email = "xxx";
        "email_privado" = "xxx";
        empresa = "Colsultorio Medico";
        "fecha_ingreso" = "2017-11-01";
        "id_directorio" = 98;
        imagen = "o8PRsaEdgar.jpg";
        nombre = ;
        "num_rotario" = "";
        padrino = "xxx";
        "tel_casa" = "xxx";
        "tel_negocio" = "xxx";
        "usuario_app" = 0;
    }
)

我想要的是转换表视图行中的每个JSON对象。例如,每行应从JSON响应中获取字段nombreapellidoscumple

每个部分都应该使用不同的JSON响应进行填充。

请告诉我接下来应该做什么。

0 个答案:

没有答案