2016-09-01 4 views
0

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)] 
+1

fügen Sie einfach '!' Diese Werte auszupacken von Optional – Dravidian

+0

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 –

+0

überprüfen nur @NDoc –

Antwort

1

Das Problem ist, Sie müssen indexPath.section verwenden, um die für den Zugriff auf Cart Objekt von Array nicht indexParh.row, so ändern Sie Ihre cellForRowAtIndexPath Code wie folgt.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("cartcell", forIndexPath: indexPath) as! AddonCell     
    if let cartAddon = cartArray[indexPath.section].cartAddon[indexPath.row] as? CartAddon, let name = cartAddon.addonName { 

     cell.AddonNameLbl.text = name 
    } 
    else { 
     cell.AddonNameLbl.text = "Set here some default value or blank string" 
    } 
    cell.AddonPriceLbl.text = "£ 0.0" 
    return cell 
} 
+0

Bingo ..! Es hat funktioniert .. danke –

Verwandte Themen