我有一个表视图控制器,显示购物清单的名称。这些购物清单是通过购物清单类创建的,该类允许我指定购物清单的名称及其中的项目。此表视图显示大约5个购物清单(名称)。当我按下一个时,我会转到另一个表视图控制器,它显示购物清单的项目。但是,当我返回购物清单(名称)表格视图控制器并按下另一个购物清单时,它仍然显示旧购物清单中的商品,而不是按下的商品。我想我已经把问题缩小到第二个表视图控制器的cellForItemAt没有被再次调用。
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
}
}
提前致谢!
答案 0 :(得分:1)
ViewDidLoad
被调用一次。由于您ListsItemsViewController
上的ListViewController
是一个强大的属性,并且您多次导航到它,因此没有任何东西告诉它更新其视图或重新加载您的tableview,因为它没有被解除分配(所以{不会调用{1}}和ViewDidLoad
委托/数据源方法。您还没有添加任何自定义代码来告知它更新。
如果您希望将这些视图控制器保留为TableView
的类属性,则可以在ListViewController
tableView.reloadData
方法中添加ViewWillAppear
,如下所示。
ListsItemsViewController
另一个选项是删除那些惰性变量,并根据需要初始化这些视图控制器,以便在从堆栈中删除它们后将其取消分配。这意味着您的tableviews将始终重新加载,而无需在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
}
//This will always get called when this view controller's view is added to the window
//so you can guarantee it will call your delegate methods to reload the tableView with your new data
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated: animated)
tableView.reloadData()
}
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
}
}
ViewWillAppear