无法将JSON数组字典放入我的UITableView单元的类对象中

时间:2015-12-28 23:15:50

标签: ios json swift2

我有一些JSON我正在迭代(它包含一个字典数组)并添加到我自己制作的自定义对象数组中,以便处理数据并向我的{{1}添加字段} 细胞。

我正在页面顶部创建一个自定义对象数组:

UITableView

在我var warehouseItems: [Inventory] = []; 我有以下内容:

viewDidLoad()

由于某种原因,它不会将项目添加到let urlString = "myUrl.php"; let session = NSURLSession.sharedSession(); let url = NSURL(string: urlString)!; session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in if let responseData = data { do { let json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, AnyObject>; //print(json); if let inventoryDictionary = json["inventory"] as? [Dictionary<String, AnyObject>] { //print(inventoryDictionary); for anItem in inventoryDictionary { if let id = anItem["id"] as? Int, let item = anItem["item"] as? String, let description = anItem["description"] as? String, let quantityOnHand = anItem["quantityOnHand"] as? Int, let supplierId = anItem["supplier_id"] as? Int, let supplierName = anItem["supplierName"] as? String { let item = Inventory(id: id, item: item, description: description, quantityOnHand: quantityOnHand, supplierId: supplierId, supplierName: supplierName); self.warehouseItems.append(item); } } print(self.warehouseItems); //This just prints 'Warehouse.Inventory' for every item I have } } catch { print("Could not serialize"); } } }.resume() 对象。我的tableView是空白的。它仅在我手动将字符串添加到单元格时才有效,因此我知道我的tableView已正确连接。

当我Inventory时,它只显示print(self.warehouseItems) 6次(我的JSON正在返回的当前项目数)。事实上,如果我Warehouse.Inventory我得到以下内容:

print(inventoryDictionary)

这是我的tableview协议,我正在尝试将对象的属性分配给我的行:

[["item": Item 1, "supplierName": Supplier 1, "quantityOnHand": 42, "id": 1, "supplier_id": 1, "description": Description], 
["item": Item 2, "supplierName": Supplier 1, "quantityOnHand": 1001, "id": 2, "supplier_id": 1, "description": Description], ... and so on

如果您需要它,这是我的自定义func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return warehouseItems.count; //Make sure to set count of array from json } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell; let row = indexPath.row; cell.textLabel!.text = warehouseItems[row].item; cell.detailTextLabel!.text = warehouseItems[row].description; return cell; } 对象类:

Inventory

我的头开始旋转,因为我切换到快速,尤其是所有选项和展开。

0 个答案:

没有答案