2016-09-01 45 views
0

我有以下類模型的項目。我在部分中顯示itemsName,itemprice。並根據索引在單元格中添加Addname和AdddonPrice ..節的數量和單元的數量從類模式正確地到來。節數據也工作正常。問題是將插件數據顯示到customTableCell中。從類模型錯誤中填充tableView單元格中的項?

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 

    } 

} 

泰伯維代碼

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

只需添加'!'解開這些值以擺脫可選 – Dravidian

+0

那不是問題..仔細查看數據。我只向你發送我正在保存到類模型中的數組格式類型。現在從那我需要顯示在給定的單元格。可選的。我知道要處理它。 @Dravidian –

+0

只是檢查@NDoc –

回答

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 
} 
+0

賓果..!它工作兄弟..謝謝 –

相關問題