0

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
    }

}
koen
  • 5,383
  • 7
  • 50
  • 89
Nik F
  • 71
  • 1
  • 11
  • What's the value of `meals.count` when you return it? – Phillip Mills Jan 11 '17 at 18:45
  • to answer your question we need also the code for the embedded TableView – muescha Jan 11 '17 at 19:18
  • i dont see any TableView inside the cell(from your current code) – muescha Jan 11 '17 at 19:19
  • without knowing your exact code from embedded TableView: in `tableView(tableView:cellForRowAt)` normally you need to update the datasource of the embedded TableView and reload it with `reloadData()` – muescha Jan 11 '17 at 19:20
  • I have added the additional code from the viewController, parent tableViewCell & chill TableVewCell – Nik F Jan 11 '17 at 19:40
  • Possible duplicate of [Is it possible to add UITableView within a UITableViewCell](http://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell) – koen Jan 11 '17 at 19:45
  • I accidentally din't include my tableView outlet when I copied from code. I have added it in now – Nik F Jan 11 '17 at 19:45

1 Answers1

0

First, I believe that it's a better idea if you separate the code, one file for you tableView and one for you tableViewCell. Then, in your tableViewCell implement theUITableViewDelegate and UITableViewDataSource. Also, create the outlet for this tableView.

//
//  NewTableViewCell.swift
//  JustATest
//
//  Created by Alisson Enz Rossi on 1/11/17.
//  Copyright © 2017 com.Cotival.justATest. All rights reserved.
//

import UIKit

class NewTableViewCell: UITableViewCell {

    @IBOutlet var tableView: UITableView!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}
extension NewTableViewCell: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath)

        //...

        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

}

Also, create an array that you can fill this tableView, same as you did in your parent controller, and don't forget to change numberOfRowsInSection to count the number of items in your array. It should work.

Alisson Enz
  • 145
  • 1
  • 8