Ich habe das folgende Klassenmodell für die Elemente. Ich zeige itemsName, itemprice in Abschnitten. Und Addonname und AdddonPrice in der Zelle basierend auf dem Index .. die Anzahl der Abschnitte und die Anzahl der Zellen kommen korrekt aus der Klasse modal. Abschnitte Daten funktionieren auch gut. Das Problem besteht darin, Addon-Daten in customTableCell anzuzeigen.Füllen Sie Elemente in der TableView-Zelle aus einem Klassenmodellfehler?
class Cart{
var itemID:AnyObject?
var itemName:AnyObject?
var itemPrice:AnyObject?
var cartAddon:[AnyObject?]
init(itemID:AnyObject?,itemName:AnyObject?,itemPrice:AnyObject?,cartAddon:[AnyObject?]){
self.itemID = itemID
self.itemName = itemName
self.itemPrice = itemPrice
self.cartAddon = cartAddon
}
}
class CartAddon {
var addonID:Int?
var addonName:String?
var addonPrice:Double?
init(addonID:Int?,addonName:String?, addonPrice:Double?){
self.addonID = addonID
self.addonName = addonName
self.addonPrice = addonPrice
}
}
Tableview Code
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return cartArray.count
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableCellWithIdentifier("cartheadercell") as! cartHeaderCell
header.ItemnameLbl.text = cartArray[section].itemName as? String
header.ItemPriceLbl.text = (cartArray[section].itemPrice as! String)
return header
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cartArray[section].cartAddon.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 30
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cartcell", forIndexPath: indexPath) as! AddonCell
//error in this part
let cart: Cart = cartArray[indexPath.row].cartAddon[indexPath.row]
if let name = cart.cartAddon[indexPath.row]!["AddonName"]{
cell.AddonNameLbl.text = (name as! String)
}
cell.AddonPriceLbl.text = "£ 0.0"
return cell
}
Die Daten werden in dieser Form kommen, die ich angezeigt ist:
Optional(9 Inch Thin & Crispy Margarita)
Optional(£3.40)
Optional(1749)
[Optional(Chicos_Pizza.CartAddon), Optional(Chicos_Pizza.CartAddon), Optional(Chicos_Pizza.CartAddon), Optional(Chicos_Pizza.CartAddon)]
fügen Sie einfach '!' Diese Werte auszupacken von Optional – Dravidian
loszuwerden, die nicht das Problem .. die Daten sorgfältig sehen. Ich sende Ihnen nur wat Art des Matrixformats, das ich in das Klassenmodell speichere. Jetzt muss ich in der angegebenen Zelle anzeigen. wahlweise. Ich weiß, damit umzugehen. @Dravidian –
überprüfen nur @NDoc –