【发布时间】:2018-05-19 00:09:32
【问题描述】:
我正在尝试制作一个家庭自动化的 IOS 应用程序。我正在使用 TableViewCell 来显示信息。
我的问题是我不知道如何使用 struct 将 JSON 转换为 Array,因为我认为必须有 struct。
我的 JSON 是:
[{"namea":"TV","statea":"up_tv"},{"namea":"test","statea":"test"}]
我的代码是:
struct cellData {
let nameLabel : String!
let stateLabel : String!
}
class Main: UITableViewController {
var array = [cellData]()
override func viewDidLoad() {
array = [cellData(nameLabel: "tv", stateLabel: "up_tv"),
cellData(nameLabel: "tv", stateLabel: "down_tv")]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
cell.nameLabel.text = array[indexPath.row].nameLabel
cell.stateLabal.text = array[indexPath.row].stateLabel
return cell
}
【问题讨论】:
-
我使用
HandyJSON。只需播下它,它就会让您的生活更轻松 -
如何使用 HandyJSON
-
在他们的 github 页面上阅读 .md