如何使这些tableView节折叠和展开?

时间:2019-05-17 04:52:05

标签: ios swift uitableview

基本上,我有以下tableView从JSON获取其数据,且各节正常工作,但是现在我试图使这些节在按下某个节时展开和折叠。可以轻松实现吗?我了解到可能需要编写自定义类,但是将JSON数据用作标头似乎更为复杂。

我的问题与被列为重复项的问题不同,因为它专门处理JSON,从而使流程变得有些复杂。

以下是我到目前为止的内容:

import UIKit

class ToCustomerTableViewController: UITableViewController {

    var sections = [Section]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
        fetchJSON()


    }

    func fetchJSON(){
        let urlString = "https://www.example.com/example/example.php"
        guard let url = URL(string: urlString) else { return }
        URLSession.shared.dataTask(with: url) { (data, _, error) in
            DispatchQueue.main.async {
                if let error = error {
                    print("Failed to fetch data from url", error)
                    return
                }
                guard let data = data else { return }
                do {
                    let decoder = JSONDecoder()
                    decoder.keyDecodingStrategy = .convertFromSnakeCase
                    let res = try decoder.decode([Portfolios].self, from: data)
                    let grouped = Dictionary(grouping: res, by: { $0.customer })
                    let keys = grouped.keys.sorted()
                    self.sections = keys.map({Section(name: $0, items: grouped[$0]!)})

                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                } catch {
                    print("Failed to decode json", error)
                }

            }
            }.resume()
    }




        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            self.accessoryType = selected ? .checkmark : .none
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let section = sections[section]
        return section.items.count
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section].name
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
        let section = sections[indexPath.section]
        let item = section.items[indexPath.row]
        let titleStr = "\(item.serial) - \(item.manufacturer) - \(item.model)"
        cell.textLabel!.text = titleStr


        return cell
    }



    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }

}

0 个答案:

没有答案