修改 UITableView 中的 JSON 数组 [英] Modifying a JSON array in a UITableView

查看:23
本文介绍了修改 UITableView 中的 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法理解在 UITableView 中呈现的 JSON 数组是如何编辑的?

I can't seem to understand how a JSON array be edited when presented in a UITableView?

The JSON array looks like the following example:

[
    {
        "customer": "John",
        "status": "Yes",
    },
    {
        "customer": "James",
        "status": "No",
    },
    {
        "customer": "Jamie",
        "status": "No",
    }
]

应用当前在 UITableView 中呈现数据的方式:

private func fetchJSON() {

    guard let url = URL(string: "https://example/example/example"),
        let value = driver.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)
        else { return }

    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.httpBody = "driver=\(value)".data(using: .utf8)

    URLSession.shared.dataTask(with: request) { data, _, error in
        guard let data = data else { return }

        do {
            self.structure = try JSONDecoder().decode([Structure].self,from:data)
            DispatchQueue.main.async {
                self.tableView.reloadData()

            }
        }
        catch {
            print(error)
        }
        }.resume()

} 

到目前为止的 TableView 委托代码:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering() {
        return pickup.count
    }
    print(structure.count)
    return structure.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "testingCell", for: indexPath)


    let portfolio: Structure

    portfolio = structure[indexPath.row]

    return cell

}

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

}

我希望能够选择客户名称 James 和 Jamie,然后按下一个按钮,将状态从否"更改为是".

I would like to be able to select the customers names James and Jamie and press a button that will change the status from No to Yes.

如何从应用的角度创建选择和更新机制?

更新:

结构定义为:var structure = [Structure]()

import UIKit

struct Structure: Codable {
    let customer: String
    let status: String
}

更新 2:

我正在使用以下内容,但似乎无法更新 API:

I am working with the following but I cannot seem to update the API:

func saveToDB(_ structure: Structure) {
    do{
        let data = try JSONEncoder().encode(structure)
        if let str = String(data: data, encoding: .utf8) {
            print(str)

                guard let url = URL(string: "https://example/example/example"),
        else { return }
        }
    } catch {
        print(error)
    }
}

推荐答案

希望这能解决您的问题

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "testingCell", for: indexPath)

        let portfolio: Structure = structure[indexPath.row]
        cell.accessoryType = portfolio.status == "YES" ? .checkmark : .none

        return cell

    }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let status = structure[indexPath.row].status // get current status
    structure[indexPath.row].status = status == "YES" ? "NO" : "YES" /// toggle status
   tableView.reloadData()
}

不需要 didDeselectRowAt 方法.

这篇关于修改 UITableView 中的 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆