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

时间:2016-01-20 20:59:19

标签: json swift2 tableview alamofire swifty-json

我想要使用alamofire和siftyJSON发送到Table View的(大量)数据

网络请求:

CREATE temp LIKE table;
ALTER TABLE temp ADD r FLOAT;

INSERT INTO temp (x,y) SELECT * FROM table WHERE x*x+y*y<1;
UPDATE temp SET r=x*x+y*y;

代码Alamofire和SwiftyJSON:

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

代码表视图:

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

            }

          //  }               

         }
        }

}  

}

网络请求:enter link description here

我试图将一些json数据发布到表视图,但是当我发送数据时它什么也没有返回。为什么??

2 个答案:

答案 0 :(得分:5)

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

enter image description here

我的代码在这里

import UIKit

class customcell:UITableViewCell {

@IBOutlet weak var lbl1: UILabel!

@IBOutlet weak var lbl2: UILabel!

}

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

}

答案 1 :(得分:1)

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

    }

}

这是您可以使用多个tableview显示数据的方法。