【问题标题】:How to use two tableView in Single viewController on Tabbar如何在 Tabbar 上的 Single viewController 中使用两个 tableView
【发布时间】:2018-03-30 05:05:13
【问题描述】:

然而,当我实现第一个 tableView 并且它工作正常时,我尝试在 Tabbar 上的同一个 viewController 上实现另一个 tableView,然后发生崩溃。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

【问题讨论】:

  • 你能告诉你的代码你试过@iOS吗
  • @DilipTiwari,我什么都没做,我只是将第二个 tableView 添加到标签栏下的 ViewController 并运行应用程序。
  • 当我在标签栏下使用单个 tableView 时它正在工作
  • stackoverflow.com/questions/23526756/… 或者请分享演示代码
  • 显示您的故事板的屏幕截图,其中选择了两个表,并且属性检查器必须在其中可见。我认为您缺少单元格标识符。另一件事是使用单元格并错过了向表格视图注册单元格类。

标签: ios swift tableview


【解决方案1】:

当您将两个表视图放在同一个控制器中时,两个表视图的委托/数据源将连接到同一个控制器。您将必须确定必须更新哪个表视图。您可以通过获取两个表视图的出口,然后在 cellForRowAt 中应用一个简单的条件构造来做到这一点。

这就是我的实现方式。

代码

    extension ViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == firstTableView
        {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeOne", for: indexPath) as? UITableViewCell
            {
                cell.textLabel?.text = "First table view. Row \(indexPath.row)"
                return cell
            }
            else
            {
                let cell = UITableViewCell.init(style: .default, reuseIdentifier: "prototypeOne")
                cell.textLabel?.text = "First table view. Row \(indexPath.row)"
                return cell
            }
        }
        else if tableView == secondTableView
        {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeTwo", for: indexPath) as? UITableViewCell
            {
                cell.textLabel?.text = "Second table view. Row \(indexPath.row)"
                return cell
            }
            else
            {
                let cell = UITableViewCell.init(style: .default, reuseIdentifier: "prototypeTwo")
                cell.textLabel?.text = "Second table view. Row \(indexPath.row)"
                return cell
            }
        }
        return UITableViewCell()
    }
}

输出

【讨论】:

    【解决方案2】:

    在 Storyboard 中选择您的原型单元格,并在右侧的 Inspectors 中将标识符设置为 Cell。 请注意,此标识符与单元类不同。

    【讨论】:

    • 请注意我使用的是 Tabbar
    • 这无关紧要。您的具体错误表明您正在尝试将具有标识符“Cell”的单元格出列,但该标识符未定义给任何单元格。如果您将该标识符添加到单元格中,您的错误应该会消失。
    • 如果您使用两个表视图,请向它们添加出口并使用if 语句与 tableView 参数进行比较。据此,使用一个或另一个标识符出队。也许您只在一个 tableView 上定义了标识符,而在另一个上出列时会产生错误。
    【解决方案3】:

    在第二个uitableview中注册相同的uitableviewcell。

    【讨论】:

      【解决方案4】:

      将您的两个表与 tableview 对象进行比较:

      创建两个表的 IBOutlet 假设 table1 和 table2 然后在 tableview 的任何委托/数据源方法中比较 tableview 并执行代码

      例如。 :我已经为 2 个 tableview 定义 numberOfRowsInSection

        - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      if(tableView == self.table1)
      {
      return self.menuTitles.count;
      }
      else
      {
      return ModelCollection.count;
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 2011-11-10
        • 2013-07-06
        • 1970-01-01
        • 2012-09-13
        • 2014-03-25
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多