2016-04-22 73 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