2016-04-22 56 views
0

我已經在自定義的UITableview單元格中有3個標籤,我試圖傳遞json數據我從api與Alamofire獲得,但我努力瞭解如何將返回的json推入tableview。任何幫助將不勝感激。下面 代碼:如何將Json數據推送到UITableView中的標籤?

import UIKit 
import Parse 
import Alamofire 

class LeagueTableController: UIViewController, UITableViewDataSource,  UITableViewDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Alamofire.request(.GET, "https://api.import.io/store/connector/88c66c----9b01-6bd2bb--d/_query?input=webpage/url:----") .responseJSON { response in // 1 
     print(response.request) // original URL request 
     print(response.response) // URL response 
     print(response.data)  // server data 
     print(response.result) // result of response serialization 

     if let JSON = response.result.value { 
      print("JSON: \(JSON)") 
     } 
    } 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

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


} 

返回的JSON這樣的:

{ 
connectorGuid = "88c66cb4-e64f-4316-9b01-6bd2bb2d762d"; 
connectorVersionGuid = "8aedfe43-948a-4559-b279-d3c3c28047a4"; 
cookies =  (
); 
offset = 0; 
outputProperties =  (
      { 
     name = team; 
     type = URL; 
    }, 
      { 
     name = played; 
     type = DOUBLE; 
    }, 
      { 
     name = points; 
     type = DOUBLE; 
    } 
); 
pageUrl = "http://www.extratime.ie/leagues/2024/100/premier-division/"; 
results =  (
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 22; 
     "points/_source" = 22; 
     team = "http://www.extratime.ie/squads/17/"; 
     "team/_source" = "/squads/17/"; 
     "team/_text" = Dundalk; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 20; 
     "points/_source" = 20; 
     team = "http://www.extratime.ie/squads/7/"; 
     "team/_source" = "/squads/7/"; 
     "team/_text" = "Derry City"; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 17; 
     "points/_source" = 17; 
     team = "http://www.extratime.ie/squads/100504/"; 
     "team/_source" = "/squads/100504/"; 
     "team/_text" = "Galway United FC"; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 16; 
     "points/_source" = 16; 
     team = "http://www.extratime.ie/squads/29/"; 
     "team/_source" = "/squads/29/"; 
     "team/_text" = "St. Patrick's Ath"; 
    }, 
      { 
     played = 8; 
     "played/_source" = 8; 
     points = 15; 
     "points/_source" = 15; 
     team = "http://www.extratime.ie/squads/30/"; 
     "team/_source" = "/squads/30/"; 
     "team/_text" = "Cork City"; 
    }, 
      { 
     played = 8; 
     "played/_source" = 8; 
     points = 15; 
     "points/_source" = 15; 
     team = "http://www.extratime.ie/squads/3/"; 
     "team/_source" = "/squads/3/"; 
     "team/_text" = "Shamrock Rovers"; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 10; 
     "points/_source" = 10; 
     team = "http://www.extratime.ie/squads/13/"; 
     "team/_source" = "/squads/13/"; 
     "team/_text" = "Finn Harps"; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 10; 
     "points/_source" = 10; 
     team = "http://www.extratime.ie/squads/2/"; 
     "team/_source" = "/squads/2/"; 
     "team/_text" = Bohemians; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 7; 
     "points/_source" = 7; 
     team = "http://www.extratime.ie/squads/8/"; 
     "team/_source" = "/squads/8/"; 
     "team/_text" = "Sligo Rovers"; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 7; 
     "points/_source" = 7; 
     team = "http://www.extratime.ie/squads/6/"; 
     "team/_source" = "/squads/6/"; 
     "team/_text" = "Bray Wanderers"; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 5; 
     "points/_source" = 5; 
     team = "http://www.extratime.ie/squads/109/"; 
     "team/_source" = "/squads/109/"; 
     "team/_text" = "Wexford Youths"; 
    }, 
      { 
     played = 9; 
     "played/_source" = 9; 
     points = 5; 
     "points/_source" = 5; 
     team = "http://www.extratime.ie/squads/15/"; 
     "team/_source" = "/squads/15/"; 
     "team/_text" = "Longford Town"; 
    } 
); 

} 我想只是推 「打」, 「點」 和 「團隊/ _text」 結果到每個的標籤。

回答

1

因爲這個問題是非常廣泛的,但沒有指明究竟是什麼問題就在這裏,一般的步驟是:

1)映射你的JSON字典/ NSDictionary中。假設JSON段,您發佈的JSONArray在下面的格式一大塊[{}],所有你需要做的是:

var arrayOfDictionaries:NSArray = NSJSONSerialization.JSONObjectWithData(yourData, options: nil, error: nil) as! NSArray 

其中yourData變量數據從網絡鑄造到NSData的格式

2下載)在您的自定義創建網點,這三個標籤tableViewCell

3)針對每個小區的套內

FUNC的tableView這些標籤(的tableView:UITableView的,的cellForRowAtIndexPath不知疲倦XPATH:NSIndexPath)

方法如下:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:YourCustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as YourCustomCell 
    cell.firstLabel.text = yourDictionary["played"] 
    cell.secondLabel.text = yourDictionary["points"] 
    cell.thirdLabel.text = yourDictionary["team"] 
    return cell 
} 

4)我想你將需要更多的細胞不是僅一個,則存儲許多字典在陣列並訪問這樣每個元素:

cell.firstLabel.text = arrayOfDictionaries[indexPath.row]["played"] 
+0

不在家,現在,我會回來的在一個小時內 – DCDC

+0

@ShaneN看到我的編輯 – DCDC

+0

認爲會,嘗試一下 – DCDC

0

您需要創建UITableViewCell的子類,比如說MyTableViewCell,然後添加一個名爲JSON的屬性。

現在,因爲你可能使用界面生成器來定義你的細胞及其再利用標識符(「細胞」),該小區的類設置爲新創建的MyTableViewCell和新定義的類連接標籤,一些IBOutlets

然後,當您調用'dequeueReusableCellWithIdentifier'時,將單元格轉換爲MyTableViewCell並將其JSON屬性設置爲您希望在單元格中具有的值。 您可能想要對更改做出反應,因此請添加didSet屬性觀察器。

var JSON:[String: AnyObject] = [String: AnyObject]() { 
    didSet { 
     print("populate your labels with your new data"); 
    } 
} 
+0

看看這個答案:http://stackoverflow.com/a/24013852/1728118 –

0

首先,你應該創建一個包含要保存3個屬性,像模特:

class Data { 
    var team = "" 
    var point = 0 
    var teamText = "" 

    init(fromJSON json: NSDictionary) { 
     team = json["team"] as! String 
     point = json["points"] as! Int 
     teamText = json["team/_text"] as! String 
    } 
} 

在你LeagueTableController,創建一個數組來保存數據並顯示它的tableView:

var data = [Data]() 

Config中的tableView來顯示數據:

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! YourCustomCell 
    let row = indexPath.row 
    cell.lblA.text = data[row].team 
    cell.lblB.text = "\(data[row].point)" 
    cell.lblC.text = data[row].teamText 
    return cell 
} 

最後,解析您的響應JSON我們的數據陣列顯示到的tableView

@IBOutlet weak var tableView: UITableView! 
var data = [Data]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Alamofire.request(.GET, "https://api.import.io/store/connector/88c66c----9b01-6bd2bb--d/_query?input=webpage/url:----") .responseJSON { response in // 1 
     print(response.request) // original URL request 
     print(response.response) // URL response 
     print(response.data)  // server data 
     print(response.result) // result of response serialization 

     if let JSON = response.result.value { 
      print("JSON: \(JSON)") 
      // parse JSON to get array of data 
      let array = JSON["results"] as! [NSDictionary] 
      // map an item to a data object, and add to our data array 
      for item in array { 
       self.data.append(Data(fromJSON: item)) 
      } 
      // after mapping, reload the tableView 
      self.tableView.reloadData() 
     } 
    } 
} 

希望這有助於!

+0

你可以在這裏發佈你的響應JSON? – maphongba008

+0

我已經更新了答案! – maphongba008

相關問題