在 swift 中从 JSON 填充表格视图时的约定是什么?
What is the convention when populating a tableview from JSON in swift?
我看到有人创建了一个对象来接收 JSON 数据,然后有一个该对象的数组。并在从 JSON 接收到新数据后,对象数组更新并且 table 视图重新加载。
我该怎么做?我不太明白,但我现在需要它,因为我需要从 PHP 接收数据,然后在 Xcode 中将其解析到 table 视图中。
如果可以的话,如果您能提供任何优化建议,我将不胜感激。
我认为他们在代码中使用了 didSet 变量。
感谢阅读!
这就是 didSet 的意思吧?
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
private var dataArray: [String] = [String]() {
didSet {
myTableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
let jsonRequest: NSURLRequest = NSURLRequest(URL: NSURL(string: "yourEndPoint")!)
NSURLConnection.sendAsynchronousRequest(jsonRequest, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if let jsonArray: [[String: String]] = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves, error: nil) as? [[String: String]] {
for jsonObject in jsonArray {
if let stringFromKey: String = jsonObject["yourKey"] as String? {
self.dataArray.append(stringFromKey)
}
}
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = dataArray[indexPath.row]
return cell
}
}
您只需交换 JSON 解析和数组类型来满足您的目的
我看到有人创建了一个对象来接收 JSON 数据,然后有一个该对象的数组。并在从 JSON 接收到新数据后,对象数组更新并且 table 视图重新加载。
我该怎么做?我不太明白,但我现在需要它,因为我需要从 PHP 接收数据,然后在 Xcode 中将其解析到 table 视图中。
如果可以的话,如果您能提供任何优化建议,我将不胜感激。
我认为他们在代码中使用了 didSet 变量。
感谢阅读!
这就是 didSet 的意思吧?
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
private var dataArray: [String] = [String]() {
didSet {
myTableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
let jsonRequest: NSURLRequest = NSURLRequest(URL: NSURL(string: "yourEndPoint")!)
NSURLConnection.sendAsynchronousRequest(jsonRequest, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if let jsonArray: [[String: String]] = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves, error: nil) as? [[String: String]] {
for jsonObject in jsonArray {
if let stringFromKey: String = jsonObject["yourKey"] as String? {
self.dataArray.append(stringFromKey)
}
}
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = dataArray[indexPath.row]
return cell
}
}
您只需交换 JSON 解析和数组类型来满足您的目的