Pradip Kumar Pradip Kumar - 3 months ago 9
iOS Question

Populate items in tableView cell from a class model error?

I have the following class model for the items. I am displaying itemsName,itemprice in sections. And Addonname and AdddonPrice in cell based on the index.. the number of sections and the number of cells are coming correctly from the class modal. sections data are also working fine. The problem is to display addon data into customTableCell.

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
}


The data is coming in this form which I have to display is:

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

Answer

The problem is you need to use indexPath.section to access the Cart object from array not indexParh.row, so change your cellForRowAtIndexPath code like this.

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
}
Comments