这是我的第一篇文章,我希望这是一个很好的问题,因为这个问题已经持续了好几天。 (从字面上搜索每一个相关的问题,并没有发现任何可以为解决方案加起来的东西。)如何遍历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
};
);
它看起来像你需要使用'offers'数组。您只需将此数组存储在'cellForRowAtIndexPath'中,就可以使用'indexPath.row'来访问数组并检索相关的商品字典。你从print(y)输出中得到什么? – Paulw11
你好,保罗!所以当我等待输入时,我一直在修补,而且我已经将代码更新为我现在拥有的代码。现在,我可以进入商品字典,但您可以在“print(m)”中看到它打印所有“商品”对象,但不再具有每个对象的关键名称“offer”。我怎样才能为每个UITableCell分离每个对象?谢谢! – brkr
因此,它看起来像m现在是你的数组,所以在cellForRowAtIndexPath中,你可以使用'm [indexPath.row]'来获得字典 – Paulw11