如何使用 Alamofire 和 SwiftyJSON 将数据发送到 table 视图

How to send data to table view with Alamofire And SwiftyJSON

我有(大量)数据要发送给 Table 使用 alamofire 和 siftyJSON 查看

网络请求:

    let postEndPoint :String = "http://jsonplaceholder.typicode.com/posts/"

编码 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 ")

            }

          //  }               

         }
        }

}  

代码table视图:

    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
}

}

网络请求:enter link description here

我正在尝试 post 一些 json 数据以 table 查看,但是当我发送数据时 returns 什么都没有。为什么??

这是适合我的代码。最主要的是你必须在 api 调用结束时重新加载 table。并且您将 nuber 打印为字符串,因此您必须将其转换为字符串

我的代码在这里

import UIKit

class 自定义单元格:UITableViewCell {

@IBOutlet weak var lbl1: UILabel!

@IBOutlet weak var lbl2: UILabel!

}

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()
    }
}

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    if (tableView == tabledata)
    {
    let cell = tableView.dequeueReusableCellWithIdentifier("customcell") as! customcell

        let dict = arr[indexPath.row] as! NSDictionary
        cell.lbl1?.text = String (dict["userId"]! )
        cell.selectionStyle = .None
        return cell
    }

   else if(tableView == tablefactoryoption)
    {
        let cell1:Facturyoptioncell = tableView.dequeueReusableCellWithIdentifier("Facturyoptioncell") as! Facturyoptioncell


        let dict = arr[indexPath.row] as! NSDictionary
        cell1.lbl2?.text = String (dict["Id"]! )
        cell1.selectionStyle = .None


  cell1.selectionStyle = .None

        return cell1

    }

    else
    {
        let cell2:Technicalcell = tableView.dequeueReusableCellWithIdentifier("Technicalcell") as! Technicalcell

        let dict = arr[indexPath.row] as! NSDictionary
        cell2.lbl3?.text = String (dict["userId"]! )
        return cell2

    }

}

这就是使用多个表格视图显示数据的方法。