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