PRADIP KUMAR PRADIP KUMAR - 5 months ago 33
Swift Question

How to populate data from class model to tableview

My data is coming in modal class named Menu by creating its object menus. Now How can i send the menu names to tableview to show in particular cell

var menus = [Menu]()
for (_, content) in json {
let menu = Menu(id: Int(content["id"].stringValue),
name: content["name"].string,
image: content["image"].string,
coupon: content["coupon"].int,
icon: content["icon"].string,
order: Int(content["order"].stringValue),
aname: content["name"].string,
options: Int(content["options"].stringValue),
subcategory:content["subcategory"].arrayObject)

menus.append(menu)
}
for menu in menus {
print(menu.name)
print(menu.id)
print(menu.subcategory)
}
print(menus.count)


Here All the data is saved in Menu class by the help of menus object.I have added the codes to show data to tableview. here i have created custom tableview and trying to populate it

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return menus.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Menucell", forIndexPath: indexPath)as! MENUTableViewCell

cell.Ordermenu.text = (" \(menus[indexPath.row])")///here its not fetching the value
return cell


}


Its not working . how the implementation should be ?

enter image description here

It shows the projectName.classname

updated after accepting answer
enter image description here

Answer

Try below line of code. Hope it will help you...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Menucell", forIndexPath: indexPath)as! MENUTableViewCell
let menu : Menu =  menus[indexPath.row]
cell.Ordermenu.text = ("  \(menu. name )")///here its not fetching the value
return cell


}