在Swift中使用NSDictionary JSON填充UITableView [英] Populate UITableView with NSDictionary JSON in Swift

查看:74
本文介绍了在Swift中使用NSDictionary JSON填充UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Fresher。我叫API,我收到数据的请求和响应。但是,我不知道如何将数据从NSDictionary填充到TableviewCell。任何人都可以帮助我。

I am Fresher.I called API and i am getting request and Response of data.But,I dont know how to populate data from NSDictionary to TableviewCell.Can anyone help me.

代码在这里:

var responseData:NSString =NSString(data:data!,encoding:NSUTF8StringEncoding)!

println("Response Data is "+responseData)
var error: NSError?

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary

println("NSDictionary JSON Is",jsonData)

var error1: NSError?

var dict: NSDictionary =["Book_name":" "]
}
}

我的回复数据是:

Response Data is {"ListOfBooks":[{"Book_name":"javascript_tutorial"}]}

(NSDictionary JSON Is, [ListOfBooks: ({"Book_name" = "javascript_tutorial";
    }
   )])

如何将文件名javascript_tutorial列入tableview?

How to List the file name "javascript_tutorial" into tableview ?

推荐答案

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary

if jsonData {
// process jsonData

var arrayofBOOKs: NSArray = jsonData["ListOfBooks"] as NSArray
for elem: AnyObject in arrayofBOOKs {
let name = elem["Book_name"] as String

println("Book_name: \(name)")
// here add the string to array 
 items.addObject(name)
}
tableView.reloadData

} else {
// couldn't load JSON, look at error
}

以下是要遵循的步骤:

Step-1

打开 ViewController.swift 类并在类声明下面添加一个新的tableview实例变量

Open the ViewController.swift class and add a new tableview instance variable below the class declaration

@IBOutlet
var tableView: UITableView!

Step-2

符合 UITableViewDelegate UITableViewDataSource 协议只需在类声明中将冒号分隔后添加到uIViewController中

To conform to the UITableViewDelegate and UITableViewDataSource protocol just add them seperated by colons afterUIViewController in the class declaration

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
    super.viewDidLoad()

    var  items = NSMutableArray()
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")


}

step-3

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    cell.textLabel?.text =  items[indexPath.row]

    return cell
}

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("You selected cell #\(indexPath.row)!")
}

这篇关于在Swift中使用NSDictionary JSON填充UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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