【问题标题】:Two Table Views In Different View Controllers one working and one not(Swift 4)不同视图控制器中的两个表视图一个工作一个不工作(Swift 4)
【发布时间】: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


【解决方案1】:

您的班级PantryListTabViewController 已经符合两种TableView 协议UITableViewDelegateUITableViewDataSource

这些协议告诉其他类这个类(PantryListTabViewController)已经实现了一些特定的方法或属性

但是,TableView 需要知道,这个类 (PantryListTabViewController) 是 TableView 应该用来填充其内容的类 和其他一些东西。

因此,在您的viewDidLoad() 中,您需要将您的类设置为 TableView 的 Delegate 和 DataSource

override func viewDidLoad() {

    // Call the super method.
    super.viewDidLoad()

    // Setup Delegate and DataSource for the TableView.
    tableView.delegate = self
    tableView.dataSource = self
}

请考虑您需要一个名为 tableView 的 TableViewIBOutlet(您显然可以重命名它)

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    要解决此问题,您需要确保为两个视图控制器正确设置了数据源和委托,您可以通过添加:

    mytableview@IBOutletvar.delegate = self
    mytableview@IBOutletvar.dataSource = self
    

    到两个视图控制器 viewDidLoad() 函数。

    在我的例子中,mytableview@IBOutletvar = ShoppingListTableView 用于购物清单视图控制器,PantryListTableView 用于食品清单视图控制器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多