J.A*_*rji 1 json tableview alamofire swifty-json swift2
我有一个(大量)数据,我想用alamofire和siftyJSON发送到Table View
网页请求:
let postEndPoint :String = "http://jsonplaceholder.typicode.com/posts/"
Run Code Online (Sandbox Code Playgroud)
代码Alamofire和SwiftyJSON:
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, postEndPoint).responseJSON { response in
// handle json
guard response.result.error == nil else{
print("error calling Get on /posts/1")
print(response.result.error)
return
}
if let value: AnyObject = response.result.value{
let post = JSON(value)
// for i in 0...post.count{
if let title = post["data"].arrayValue as? [JSON]{
self.datas = title
self.tableView.reloadData()
print("the title is :\(title)" )
}else{
print("eror parsing /post/1 ")
}
// }
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码表视图:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell")!
var dict = datas[indexPath.row]
cell.textLabel?.text = dict["userId"] as? String
cell.detailTextLabel?.text = dict["id"] as? String
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
Run Code Online (Sandbox Code Playgroud)
}
网络请求:在此处输入链接描述
我试图将一些json数据发布到表视图,但是当我发送数据时它什么都不返回.为什么??
这是为我工作的代码.主要的是你必须在api调用结束时重新加载表.并且您将nuber打印为字符串,因此您必须将其转换为字符串

我的代码在这里
import UIKit
Run Code Online (Sandbox Code Playgroud)
class customcell:UITableViewCell {
@IBOutlet weak var lbl1: UILabel!
@IBOutlet weak var lbl2: UILabel!
Run Code Online (Sandbox Code Playgroud)
}
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tabledata: UITableView!
let postEndPoint :String = "http://jsonplaceholder.typicode.com/posts/"
var arr:NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
web()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customcell") as! customcell
let dict = arr[indexPath.row] as! NSDictionary
print(dict)
print(dict["userId"])
cell.lbl1?.text = String (dict["userId"]! )
cell.lbl2?.text = String (dict["id"]! )
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func web()
{
request(.GET, postEndPoint, parameters: nil, encoding:
.JSON).responseJSON { (response:Response<AnyObject, NSError>) -> Void in
print(response.result.value)
if (response.result.value != nil)
{
self.arr = (response.result.value) as! NSMutableArray
}
self.tabledata.reloadData()
}
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
1509 次 |
| 最近记录: |