1

我正在尝试UIPickerView使用来自 API 的响应来填充一个,我可以使用基本的 JSON 响应成功地做到这一点,但我真的在努力解决下面的示例,其中 JSON 将值作为数组返回。

下面的代码来自 Playground 并已简化为仅显示相关代码,我相信如果我可以使用正确格式的数组创建一个变量,那么我可以使用它来填充UIPickerView但我已经包含了UIPickerView代码以防我以错误的方式解决这个问题。

我已经从这个论坛和其他论坛搜索并尝试调整各种示例,但由于某种原因,我无法让它工作,我是 Swift 新手,所以我处于学习曲线上!

我得到的最接近的是account(data: ["account1", "account2", "account3"]),但我需要得到["account1", "account2", "account3"]

我希望有人可以提供帮助,希望不要对这个问题投反对票。

import UIKit

let json = """
{
"returnResult":1,
"data":["account1", "account2", "account3"]
}
"""

struct account: Codable {
    var data: [String]
}

let accounts: account = try JSONDecoder().decode(account.self, from:json.data(using: .utf8)!)

print(accounts)

/*
RESULT RETURNED
account(data: ["account1", "account2", "account3"])

RESULT REQUIRED
["account1", "account2", "account3"]

PICKERVIEW CODE FROM WORKING VIEW

// picker view methods
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent    component: Int) -> Int {
return accs.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int,    forComponent component: Int) -> account? {
return accs[row]
}

// delegate method
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedDriver = accs[row]
displayLbl.text = selectedDriver
}
*/
4

1 回答 1

0

我终于使用以下代码作为解决方案的基础对其进行了排序

import UIKit

struct Account: Codable {
    var data: [String]
}

class ViewController: UIViewController {
    // MARK: - IBOutlets
    @IBOutlet private var displayLabel: UILabel!
    @IBOutlet private var pickerView: UIPickerView!

    // MARK: - Internal Variables'
    var account: Account?

    // MARK: - IBActions
    @IBAction private func showPicker(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        pickerView.isHidden = !sender.isSelected
    }

    private func createAccountList() {
        let json = """
        {
        "returnResult":1,
        "data":["account1", "account2", "account3","account4", "account5", "account6"]
        }
        """
        do {
            account = try JSONDecoder().decode(Account.self, from: json.data(using: .utf8)!)
        } catch {
            print(error.localizedDescription)
        }
    }

    private func initUI() {
        pickerView.delegate = self
        pickerView.dataSource = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        createAccountList()
        initUI()
        pickerView.reloadAllComponents()
        // Do any additional setup after loading the view, typically from a nib.
    }
}

extension ViewController: UIPickerViewDataSource, UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return account?.data.count ?? 0
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return account?.data[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        displayLabel.text = account?.data[row]
    }
}

该项目可以在 Github 上找到https://github.com/techmehra/uipicker-demo

我确实必须稍微修改代码以使其与实时 API 响应一起使用。

于 2018-09-20T05:26:14.280 回答