2016-04-20 8 views
1

MVCパターンを使用しようとしていて、Alamofire APIを使用してjsonデータとして応答を取得しました。モデルからテーブルビューを更新するためのデータを送信できません。json値でテーブルを更新してください。modelの(json)値をView Controllerに送信するにはどうすればよいですか?

これは私のAPIです:

class func showData(completionHandler:(model)->()){ 
     var arrRes = [[String:AnyObject]]() 
     Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON { (req, res, json) -> Void in 
     let swiftyJsonVar = JSON(json.value!) 
     if let resData = swiftyJsonVar["contacts"].arrayObject { 
      arrRes = resData as! [[String:AnyObject]] 
      print(arrRes) 
      if arrRes.count > 0 { 
      let name = swiftyJsonVar["contacts"]["name"].stringValue 
      let email = swiftyJsonVar["contacts"]["email"].stringValue 

       let detail = model(name: name, email: email) 
       completionHandler(detail) 

      } 
      } 
     } 
} 

これは私のコントローラです:

@IBOutlet var appsTableView: UITableView! 
    var tableData:[model] = [model]() 


    override func viewDidLoad() { 
    super.viewDidLoad() 

    self.appsTableView.reloadData() 
    self.appsTableView.backgroundColor = UIColor.orangeColor() 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = appsTableView.dequeueReusableCellWithIdentifier("MyTestCell") as! newTabelcell 
    let dict = self.tableData[indexPath.row] 
    cell.name.text = dict.name 
    cell.mailid.text = dict.email 
    return cell 
} 

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

func configureTableView() { 
    appsTableView.rowHeight = UITableViewAutomaticDimension 
    appsTableView.estimatedRowHeight = 100.0 
    self.appsTableView.reloadData() 
} 

これは私のモデルである:

class model: NSObject { 
     var name:String! 
     var email:String! 
    init(name:String , email:String){ 
     super.init() 
     self.name = name 
     self.email = email 
     } 
    } 

私はテーブルビューを持っていますし、2つのラベルがありますjsonの名前を表示するラベルとjsonの電子メールを表示するラベル。

答えて

0

私はこれは私がAlamofireを使用する方法です、私は前にこれで働いていると私はあなたの呼び出しからの違いと呼ばれるので、あなたには、いくつかの問題を取得すると思います:

Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON { (responseData) -> Void in 
      switch responseData.result { 
      case .Success(let value): 
       let swiftyJsonVar = JSON(value) 
       // You can adjust here, because I didn't use Contact Model in my Project. 
       if let resData = swiftyJsonVar["contacts"].arrayObject { 
        self.arrRes = resData as! [[String:AnyObject]] 
       } 
       if self.arrRes.count > 0 { 
        self.tblJSON.reloadData() 
       } 
      case .Failure(let error): 
       print(error.description) 
      } 
     } 

あなたはhttps://github.com/khuong291/Swift_Example_Seriesで私のプロジェクトをダウンロードすることができます(プロジェクト11 )。 JSONのtableViewへの読み込みが完了しました。

0

これは、itunessearchメソッドを持つ私の編集されたAPIクラスです。

func itunesSearch(completionHandler:(songData)->()) { 

    Alamofire.request(.GET, "http://itunes.apple.com/search?", parameters: ["term" : "tamil new songs", "media" : "music"]) 
     .responseJSON { (request, response, json) in 

       let json = JSON(json.value!) 
       if let jsonData = json["results"].arrayObject { 
       self.array = jsonData as! [[String : AnyObject]] 
       for num in 1...5{ 
       let arraydata = json["results"][num] 
        //print([arraydata]) 
       let artistid = arraydata["artistId"].stringValue 
       let trackname = arraydata["trackName"].stringValue 

       let song = songData(country: artistid , currency: trackname) // The parameter name country and currency of this method is just defined in the model class.Don't get confuse by those names which is just reference. 
       completionHandler(song) } 
      } 
     } 
} 

それはまさに私のために働いている..

関連する問題