J.Arji
J.Arji

Reputation: 661

How to send data to table view with Alamofire And SwiftyJSON

I have a (large amount) of data that I want to send to Table View with alamofire and siftyJSON

web request :

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

code Alamofire and 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 ")

            }

          //  }               

         }
        }

}  

code table view :

    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
}

}

web request :enter link description here

I am trying to post some json data to table view but when I send the data it returns nothing . why??

Upvotes: 1

Views: 2262

Answers (2)

Govind Rakholiya
Govind Rakholiya

Reputation: 295

this is the code working for me. main thing is you have to reload the table at the end of api calling. and you are printing nuber as a string so you have to convert it to string

enter image description here

my code is here

import UIKit

class customcell: 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()
    }
}

}

Upvotes: 5

Govind Rakholiya
Govind Rakholiya

Reputation: 295

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

    }

}

This is how you can use multiple tableview for display data.

Upvotes: 1

Related Questions