【发布时间】:2020-08-22 03:38:40
【问题描述】:
我的目标是构建一个应用程序,其中在两个不同的视图中有两个列表 1 称为购物清单,第二个称为食品储藏室清单。这是一个选项卡式应用程序模板。我首先使用包含表格视图的视图设置购物清单选项卡,该视图从公共变量文件中获取其数据以填充视图,这工作正常。但是,我将相同的代码复制并粘贴到第二个视图控制器中(更改所有变量和引用,包括 @IBOutlet 链接),此选项卡仅显示一个空表视图。
购物清单选项卡视图控制器
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)
}
}
食品列表标签视图控制器
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,另一个称为食品储藏室项目
如果我问这个问题很抱歉。
【问题讨论】:
-
您是否确保您的数据源有内容并正确设置?在您的代码和情节提要中都有吗?
-
修复了它,非常感谢你的惊人
标签: ios uitableview uiviewcontroller swift4 xcode11