2016-04-22 72 views
1

這是我的第一篇文章,我希望這是一個很好的問題,因爲這個問題已經持續了好幾天。 (從字面上搜索每一個相關的問題,並沒有發現任何可以爲解決方案加起來的東西。)如何遍歷UITableViewCell中字典值的字典?

基本上我構建了一個純Swift應用程序。我的問題是,我不知道如何將每個字典值放入每個創建的UITableCell。

另外,JSON響應GET創建NSCFDictionary類型。

的UITableViewCell屬性

  • 的UILabel - 保存要約 「名」
  • UI標籤#2 - 保存要約 「描述」
  • 的UIImage - 保存要約 「thumb_url」

所以基本上我需要在ViewController創建的每個UITableViewCell中存儲每個offer對象的(name, description, thumb_url)

GET Request

import UIKit 

class freeIAPCell: UITableViewCell { 

    @IBOutlet weak var aName: UILabel! 

    @IBOutlet weak var aDescription: UILabel! 

    @IBOutlet weak var aImage: UIImageView! 



} 

class FreeIAPViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 


    // Everbadge Request 

    var apiURL = "apiurlhere" 

    var parsedArray: [[String:String]] = [] 

    func queryEB() { 

     // Query DB Here 

     // Store the API url in a NSURL Object 
     let nsURL = NSURL(string: apiURL) 

     let request = NSMutableURLRequest(URL: nsURL!) 

     request.HTTPMethod = "GET" 

     // Execute HTTP Request 

     let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL!) { 
      data, response, error in 

      if error != nil { 
       print("Error = \(error)") 
       return 
      } 

      let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) 

      do { 
       let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSDictionary 

       print(json.isKindOfClass(NSDictionary)) // true 

       let data = json.objectForKey("data") as! NSDictionary 
       //print(data) 
       let m = data.objectForKey("offers") as! NSArray 
       print(m) 
       print(m.valueForKeyPath("name")) 
       self.parsedArray = m as! [[String : String]] 


      } catch { 
       print("THERE WAS AN ERROR PARSING JSON FOR EVERBADGE") 
      } 


     } 
     task.resume() 

    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     queryEB() 




    } 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(true) 


    } 

    // MARK: UITableView method implementation 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 


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

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return 100 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


    } 

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

     let obj = parsedArray[indexPath.row] // fatal error: Index out of range 
     cell.aName.text = obj["name"] 
     return cell 
    } 


} 

API Request Data Structure(印刷(M))

( 
       { 
       "name" = "Mate"; 
       id = 23941; 
       description = "10-20 word description goes here" 
       "url" = "google.com 
       "ver" = "0.12" 
       price = "0.17" 
       "public_name" = "Good Ole Mate" 
       "thumb_url" = "http://google.com/mate.jpg 
      }; 
      { 
       "name" = "Mate"; 
       id = 23941; 
       description = "10-20 word description goes here" 
       "url" = "google.com 
       "ver" = "0.12" 
       price = "0.17" 
       "public_name" = "Good Ole Mate" 
       "thumb_url" = "http://google.com/mate.jpg 
      }; 
      { 
       "name" = "Mate"; 
       id = 23941; 
       description = "10-20 word description goes here" 
       "url" = "google.com 
       "ver" = "0.12" 
       price = "0.17" 
       "public_name" = "Good Ole Mate" 
       "thumb_url" = "http://google.com/mate.jpg 
      }; 
      { 
       "name" = "Mate"; 
       id = 23941; 
       description = "10-20 word description goes here" 
       "url" = "google.com 
       "ver" = "0.12" 
       price = "0.17" 
       "public_name" = "Good Ole Mate" 
       "thumb_url" = "http://google.com/mate.jpg 
      }; 
      { 
       "name" = "Mate"; 
       id = 23941; 
       description = "10-20 word description goes here" 
       "url" = "google.com 
       "ver" = "0.12" 
       price = "0.17" 
       "public_name" = "Good Ole Mate" 
       "thumb_url" = "http://google.com/mate.jpg 
      }; 
); 
+0

它看起來像你需要使用'offers'數組。您只需將此數組存儲在'cellForRowAtIndexPath'中,就可以使用'indexPath.row'來訪問數組並檢索相關的商品字典。你從print(y)輸出中得到什麼? – Paulw11

+0

你好,保羅!所以當我等待輸入時,我一直在修補,而且我已經將代碼更新爲我現在擁有的代碼。現在,我可以進入商品字典,但您可以在「print(m)」中看到它打印所有「商品」對象,但不再具有每個對象的關鍵名稱「offer」。我怎樣才能爲每個UITableCell分離每個對象?謝謝! – brkr

+0

因此,它看起來像m現在是你的數組,所以在cellForRowAtIndexPath中,你可以使用'm [indexPath.row]'來獲得字典 – Paulw11

回答

1

首先建立公共陣列爲您3項...

var nameArray = [String]() 
var descriptionArray = [String]() 
var thumbnailArray = [String]() 

然後依次通過您的JSON解析像這樣....

,descriptionLabel: UILabel,和thumbnailImage:
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) as! NSDictionary 
    if responseString != nil 
    { 
     let items: AnyObject! = responseString["items"] as AnyObject! 
     if items != nil 
     { 
      // Loop through all search results and keep just the necessary data. 
      for var i=0; i<items.count; ++i 
      { 

       if let names = items[i]["name"] as? NSDictionary 
       { 
        let name = names as! String 
        self.nameArray.append(name) 
       } 
       if let descriptions = items[i]["description"] as? NSDictionary 
       { 
        let description = descriptions as! String 
        self.descriptionArray.append(description) 
       } 
       if let thumbnails = items[i]["thumb_url"] as? NSDictionary 
       { 
        let thumbnail = thumbnails as! String 
        self.thumbnailArray.append(thumbnail) 
       } 
      } 
     } 
     self.resultsTableView.reloadData() 

與nameLabel創建OfferTableViewCell類。最後在你的cellForRowAtIndexPath .....

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("offer", forIndexPath: indexPath) as! OfferTableViewCell 
     cell.nameLabel.text = self.nameArray[indexPath.row] 
     cell.descriptionLabel.text = self.descriptionArray[indexPath.row] 
     let url = NSURL(string: self.thumbnailArray[indexPath.row]) 
     let data = NSData(contentsOfURL: url!) 
     cell.thumbnailImage.image = UIImage(data: data!) 

     return cell 

    } 
+0

你是一個野獸!謝謝! – brkr