从类模型错误中填充tableView单元格中的项目?

时间:2016-09-01 12:30:11

标签: ios swift uitableview

我有这些项目的以下类模型。我正在显示itemsName,itemprice。基于索引的单元格中的Addonname和AdddonPrice ..从类模态中正确地得到了节数和单元格数。部分数据也正常。问题是将插件数据显示到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代码

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
}

数据以这种形式出现,我必须显示:

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 个答案:

答案 0 :(得分:1)

问题是您需要使用indexPath.section从数组Cart访问indexParh.row对象,因此请更改您的cellForRowAtIndexPath代码。

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
}