簡體   English   中英

如何快速將本地JSON文件加載到uitableview中?

[英]How do I load a local JSON file into a uitableview in swift?

我一直在嘗試尋找方法,但仍然沒有成功。 我想將JSON文件放入表格視圖

{
  "person":[
       {
         "name": "John",
         "age": "17",
       },
       {
         "name": "Bob",
         "age": "23",             
       }

   ]
}    

您可以從json字符串中獲取詳細信息,如下所示。

let listDetails = json["Person"] as? Array<[String:String]>)!

您可以按如下方式讓您的表視圖委托。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // Return the number of sections.
    return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    return listDetails.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("yourCellIdentifier", forIndexPath: indexPath)

    let dict = listDetails[indexPath.row] as? [String:String]
    let name = dict["name"]
    let age = dict["age"]

    //now assign this name and age to the textViews you have in your tableView.

    return cell
}    

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM