I have a table view controller that displays names of shopping lists. These shopping lists are created through a shopping list class, which lets me specify the name of the shopping list and items in it. This table view displays around 5 shopping lists (names). When I press one, I go to another table view controller, which shows the items of the shopping list. However, when I go back to the shopping list (names) table view controller and press another shopping list, it still displays the items from the old shopping list and not the one that was pressed. I think I have narrowed the problem down to the cellForItemAt of the second table view controller not being called again.
import UIKit
class List: NSObject {
var name: String
var items: [String?]
var shared: String?
init(name: String, items: [String], shared: String?) {
self.name = name
self.items = items
self.shared = shared
}
}
enum ListType {
case apply
case view
}
class ListViewController: UITableViewController {
//Initialize Variables Here
let cellId = "cellId"
var lists: [List] = [
List(name: "Veggies", items: ["Fruit Loops", "Pasta"], shared: nil),
List(name: "11/17/18", items: ["Eggs", "Green Beans", "Pirate Booty", "Bread", "Milk"], shared: nil),
List(name: "Fruits", items: ["Fruit Loops", "Oranges"], shared: nil),
List(name: "Red Foods", items: ["Apples", "Tomatoes", "Watermelon", "Cherries"], shared: nil),
List(name: "Grains", items: ["Bread Crumbs", "Pasta", "Rice", "Chicken Flavored Rice"], shared: nil)
]
//Create Class Lazy Vars
lazy var newListVC: NewListViewController = {
let launcher = NewListViewController()
// launcher.homeController = self
return launcher
}()
lazy var listItemsVC: ListItemsViewController = {
let launcher = ListItemsViewController()
// launcher.homeController = self
return launcher
}()
//Setup Enums and Switches
var listType: ListType!
override func viewDidLoad() {
super.viewDidLoad()
//Things that both cases have in common:
navigationController?.navigationBar.prefersLargeTitles = true
let addBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(newList)) //+ Button
addBarButtonItem.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 27)!], for: .normal)
addBarButtonItem.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 27)!], for: .selected)
//SWITCH STATEMENT
switch listType as ListType {
case ListType.apply:
//Apply Begin
navigationItem.title = "Apply List"
//Setup Navigation Bar Items
let locateBarButtonItem = UIBarButtonItem(title: "Locate Item", style: .plain, target: self, action: nil)
//Add items to navigation bar
navigationItem.rightBarButtonItems = [addBarButtonItem, locateBarButtonItem]
//Apply End
case ListType.view:
//View Begin
//Setup Navigation Bar
navigationItem.title = "My Lists"
//Add items to navigation bar
navigationItem.rightBarButtonItem = addBarButtonItem
navigationItem.leftBarButtonItem = editButtonItem
//View End
}
//SWITCH STATEMENT END
//Register TableView with Id: cellId
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
}
@objc func newList() {
newListVC.pageControlFunc(pageView: PageView.name)
navigationController?.pushViewController(newListVC, animated: true) //Then pushes listVC
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lists.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.textLabel?.text = self.lists[indexPath.row].name //Then sets cell of indexPath text = lists value of index path
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
listItemsVC.listName = lists[indexPath.row].name
listItemsVC.listItems = lists[indexPath.row].items
print(listItemsVC.listItems)
navigationController?.pushViewController(listItemsVC, animated: true)
}
}
///////////////////////////////////////////////////////////////////////////
SECOND UITABLEVIEW - NOT UPDATING
///////////////////////////////////////////////////////////////////////////
import UIKit
class ListItemsViewController: UITableViewController {
//Initialize Variables Here
let cellId = "cellId1"
var listName: String?
var listItems: [String?] = []
override func viewDidLoad() {
super.viewDidLoad()
//Things that both cases have in common:
navigationController?.navigationBar.prefersLargeTitles = true
//Set Title
navigationItem.title = listName
//Register TableView with Id: cellId
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
//Delegate method not called on push to view
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.textLabel?.text = listItems[indexPath.row] //Then sets cell of indexPath text = lists value of index path
return cell
}
}
Thanks in advance!