两个 Table 视图在不同的视图控制器中,一个工作,一个不工作(Swift 4)
Two Table Views In Different View Controllers one working and one not(Swift 4)
我的目标是构建一个应用程序,其中在两个不同的视图中有两个列表 1 称为购物列表,第二个是食品储藏室列表。这是在选项卡式应用程序模板中。我首先使用包含 table 视图的视图设置购物清单选项卡,该视图从公共变量文件获取数据以填充视图,这工作正常。但是,我将相同的代码复制并粘贴到第二个视图控制器中(更改所有变量和引用,包括 @IBOutlet Link)并且此选项卡仅显示一个空的 table 视图。
购物清单选项卡视图控制器
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 列表选项卡视图控制器
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)
}
}
它们都引用同一个公用文件,该文件包含两个列表,一个名为 ShoppingList,另一个名为 pantry items
对不起,如果我问了这个问题。
要解决此问题,您需要确保为两个视图控制器正确设置了数据源和委托,您可以通过添加:
mytableview@IBOutletvar.delegate = self
mytableview@IBOutletvar.dataSource = self
到两个视图控制器 viewDidLoad() 函数。
在我的例子中,mytableview@IBOutletvar = ShoppingListTableView 用于购物列表视图控制器,PantryListTableView 用于食品储藏室列表视图控制器。
您的 class PantryListTabViewController
已经符合 两个 TableView 协议(UITableViewDelegate
和 UITableViewDataSource
)
These protocols tell other classes that this class (PantryListTabViewController
) has implemented some specific methods or properties
但是,TableView 需要知道,这个 class (PantryListTabViewController
) 是 class TableView 应该用来填充它的内容 和其他一些东西。
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
}
请考虑您的 TableView
中需要一个名为 tableView 的 IBOutlet
(您显然可以重命名它)
希望对您有所帮助!
我的目标是构建一个应用程序,其中在两个不同的视图中有两个列表 1 称为购物列表,第二个是食品储藏室列表。这是在选项卡式应用程序模板中。我首先使用包含 table 视图的视图设置购物清单选项卡,该视图从公共变量文件获取数据以填充视图,这工作正常。但是,我将相同的代码复制并粘贴到第二个视图控制器中(更改所有变量和引用,包括 @IBOutlet Link)并且此选项卡仅显示一个空的 table 视图。
购物清单选项卡视图控制器
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 列表选项卡视图控制器
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)
}
}
它们都引用同一个公用文件,该文件包含两个列表,一个名为 ShoppingList,另一个名为 pantry items
对不起,如果我问了这个问题。
要解决此问题,您需要确保为两个视图控制器正确设置了数据源和委托,您可以通过添加:
mytableview@IBOutletvar.delegate = self
mytableview@IBOutletvar.dataSource = self
到两个视图控制器 viewDidLoad() 函数。
在我的例子中,mytableview@IBOutletvar = ShoppingListTableView 用于购物列表视图控制器,PantryListTableView 用于食品储藏室列表视图控制器。
您的 class PantryListTabViewController
已经符合 两个 TableView 协议(UITableViewDelegate
和 UITableViewDataSource
)
These protocols tell other classes that this class (
PantryListTabViewController
) has implemented some specific methods or properties
但是,TableView 需要知道,这个 class (PantryListTabViewController
) 是 class TableView 应该用来填充它的内容 和其他一些东西。
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
}
请考虑您的 TableView
中需要一个名为 tableView 的 IBOutlet
(您显然可以重命名它)
希望对您有所帮助!