I'm trying to integrate embedded table views where that the UITableViewCell
of the parent UITableView
contains another custom Table View. I am able to populate the other content of the parent table view cell(e.g. the label and an image view.). However, the child UITableView
doesn't get populated with it's content and remains blank.
This is the code from the main UIViewController
that is the "parent" of the parent UITableView
:
//table view delegate function
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return completedOrders.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "completedOrdersCell", for: indexPath) as! CompletedOrdersTableViewCell
let currentOrder = completedOrders[indexPath.row]
cell.orderDate.text = dateToString(date: currentOrder.date)
cell.orderTime.text = dateToTimeToString(date: currentOrder.date)
cell.totalAmountPaid.text = "R\(String(format: "%.2f", currentOrder.totalCost))"
cell.driverProfile.image = currentOrder.driverProfile
cell.driverName.text = currentOrder.driverName
cell.restaurantLocation.text = currentOrder.restaurantLocation
cell.dropOffAddress.text = currentOrder.dropOffAddress
cell.mealsJSON = currentOrder.mealsJSON
cell.deliveryFee.text = "R\(currentOrder.deliveryFee)"
cell.tip.text = "R\(String(format: "%.2f", Double(currentOrder.driverTip)))"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
completedOrderTableView.deselectRow(at: indexPath, animated: false)
}
Here is my code in the parent TableViewCell:
@IBOutlet var mealsTableView: UITableView!
var mealsJSON = ""
struct Meal{
var quantity = Int()
var name = ""
var price = Double()
}
var meals = [Meal]()
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return meals.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mealCell", for: indexPath) as! completedMealTableViewCell
let currentMeal = meals[indexPath.row]
cell.quantity.text = "\(currentMeal.quantity)"
cell.name.text = currentMeal.name
cell.amount.text = "R\(String(format: "%.2f", currentMeal.price))"
return cell
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
mealsTableView.delegate = self
mealsTableView.dataSource = self
print(mealsJSON)
jsonToMeal()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func jsonToMeal(){
//set meals to data
let mealJSONData = mealsJSON.data(using: String.Encoding.utf8)
let readableJSON = JSON(data: mealJSONData!)
//extract array from JSON
for item in readableJSON["meals"].arrayValue{
var newMeal = Meal()
newMeal.quantity = item["mealQuantity"].intValue
print(newMeal.quantity)
newMeal.name = item["mealName"].stringValue
print(newMeal.name)
newMeal.price = item["mealPrice"].doubleValue
print(newMeal.price)
meals.append(newMeal)
}
mealsTableView.reloadData()
}
Here is the code of the child UITableViewCell
:
class completedMealTableViewCell: UITableViewCell {
@IBOutlet var quantity: UILabel!
@IBOutlet var name: MarqueeLabel!
@IBOutlet var amount: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}