I'm aiming to build an app where there are two lists in two different views 1 is called Shopping List and the second pantry list. This is in a tabbed application template. I first set up the shopping list tab with a view containing a table view which gets its data to fill the view from a common variable file and this works fine. However, I copied and pasted the same code into the second view controller(Changing all the variables and references INCLUDING the @IBOutlet Link) and this tab shows only an empty table view.
Shopping list Tab View Controller
class ShoppingListTabViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var ShoppingListTableView: UITableView!
//Set the # of rows in the shopping List
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //Set the # of rows
return(Common.Global.shoppingList.count)
}
//Define what a cell looks like
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Create a cell with the default style and the id of the Table in the Shopping List tab
let shoppinglistItem = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "shoppingListCell")
shoppinglistItem.textLabel?.text = Common.Global.shoppingList[indexPath.row] //Fill the table with the items from the shoppingList
return(shoppinglistItem)
}
}
Pantry List Tab View Controller
class PantryListTabViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var PantryListTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Set the number of rows in table
return(Common.Global.pantryItems.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Setup the cell which each item will be stored in
let pantryItemsCell = tableView.dequeueReusableCell(withIdentifier: "PantryListItem", for: indexPath)
pantryItemsCell.textLabel!.text = Common.Global.pantryItems[indexPath.row]
return(pantryItemsCell)
}
}
They both reference the same Common file which contains two lists one called ShoppingList and the other pantry items
I'm sorry if I stuffed up asking the question.
Your class PantryListTabViewController
is already conforming to both TableView Protocols (UITableViewDelegate
and UITableViewDataSource
)
These protocols tell other classes that this class (
PantryListTabViewController
) has implemented some specific methods or properties
However, the TableView needs to know, that this class (PantryListTabViewController
) is the class the TableView should use to populate it's content and some other things.
Therefore, in your
viewDidLoad()
, you need to set up your class as the Delegate and DataSource for the TableView:
override func viewDidLoad() {
// Call the super method.
super.viewDidLoad()
// Setup Delegate and DataSource for the TableView.
tableView.delegate = self
tableView.dataSource = self
}
Please consider that you need an IBOutlet
of your TableView
called tableView (you can obviously rename it)
Hope this helps!