2015-10-13 2 views
0

Я делаю этот код. Как решить эту проблему после этого кода.Как загрузить данные в ячейке UITableView из Api в swift

import Foundation 
import SwiftyJSON 
import Alamofire 

class SymptomFieldsName { 

var name: String? 
var slug: String? 
var msp_enabled: Bool? 
var results: Array<String>? 


required init(json: JSON, id: Int?) { 

    self.name = json[RecordFields.Name.rawValue].stringValue 
    self.slug = json[RecordFields.Slug.rawValue].stringValue 
    self.msp_enabled = json[RecordFields.Mspenabled.rawValue].boolValue 

    if let jsonArray = json[RecordFields.Results.rawValue].array 
    { 

     self.results = Array<String>() 
     for entry in jsonArray { 
      self.results?.append(entry.stringValue) 
     } 
    } 
} 

// MARK: Endpoints 
func endpointForSpecies() -> String 
{ 
    return "http://52.74.163.60/api/symptoms_list/" 
} 

func getSpeciesAtPath(path: String, completionHandler: (SymptomName?, NSError?) -> Void) { 
    Alamofire.request(.GET, path) 
     .responseSpeciesArray { (request, response, symptomName, error) in 
      if let anError = error 
      { 
       completionHandler(nil, error) 
       return 
      } 
      completionHandler(symptomName, nil) 
    } 
} 
+0

Где ваш код для UITableViewCell? –

+0

@IBOutlet слабый var tableview: UITableView! функ Tableview (Tableview: UITableView, numberOfRowsInSection раздел: Int) -> Int { функ Tableview (Tableview: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { пусть клетка = tableView.dequeueReusableCellWithIdentifier ("Cell", forIndexPath : indexPath) как! UITableViewCell функ Tableview (Tableview: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { } –

+0

Я хочу, чтобы получить данные в UITableView из Апи в стрижа. поэтому plg требует код –

ответ

1

Использование NSJSONSerialization

let request = NSURLRequest(URL: <# JSON file URL #>) 

let session = NSURLSession.sharedSession() 

let task = session.dataTaskWithRequest(request) { 

(data, response, error) -> Void in 

if error == nil{ 
    jsonArray = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves) as! [NSDictionary] 

    // That way the entire JSON file is stored in an array of Dictionaries which can be accessed with key : value pairs 

     } 
    } 
    task.resume() 
0

Вы можете попробовать следующие детали: объяснения

import Foundation 
import SwiftyJSON 
import Alamofire 

class SymptomFieldsName { 

let path = "http://52.74.163.60/api/symptoms_list/" 
    var ArrayOfString : [String] = [] // array of String data type. 
    @IBOutlet weak var tableview: UITableView!  

    func getSpeciesAtPath(path: String) -> Void) { 

//Step 2: Alamofire request call. 
    Alamofire.Manager.sharedInstance.request(.GET, path, parameters: nil, encoding: .JSON).responseJSON() 
      { 
       (request, responce, result) in 

       if(result.isSuccess) 
       { 

        //Step 3: We have used Swifty JSON to convert result into json array. 
        let json1 = JSON(result.value!) 

        self.ArrayOfString = json1["stringvalue"].stringValue 


       } 
      } //Alamofire ends here 
    }//Function ends here 


//MARK: UITableViewDataSource Function 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 

     print(indexPath.row) // Here you will get elements in `ArrayOfString` array 
     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print(indexPath) // This will return selected row index. 
    } 





}//Class ends here 

ArrayOfString содержит ваши результирующие значения строки. Надеюсь, что эта подробная информация будет работать для вас.

Смежные вопросы