【问题标题】:Two Table Views in One VC一个 VC 中的两个表视图
【发布时间】:2018-09-03 11:23:41
【问题描述】:

我想从一个视图控制器显示两个(或更多)表视图。我的意思是同时显示,例如,彼此并排或一个在另一个之上。我能想到的唯一方法是使用子视图控制器。有没有更好或更简单的方法?

感谢您的 cmets。

【问题讨论】:

  • 有可能做到这一点,这几乎是一个有效的情况,但你有什么问题?您如何定义更好更容易?如果你能先介绍一下你的想法/概念,那就太好了——因为目前这个问题主要是基于意见的 atm。

标签: swift uitableview uiviewcontroller swift4


【解决方案1】:

是的,当然你可以这样使用:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
    if tableView == firstTableView
    {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell1", for: indexPath)
            cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"
            return cell
    }
    else if (tableView == secondTableView)
    {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell2", for: indexPath)
            cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"
            return cell
    }
    else
    {
        return UITableViewCell()
    }
}

与其他tableview委托方法类似,你应该为每个表添加一个条件

【讨论】:

    【解决方案2】:

    如果您的用例是有两个单独滚动的表视图,那么您可以简单地在 xib 中添加两个表视图,分配数据源和委托,然后就可以结束了。

    如果您的用例是在一个页面上显示两种类型的信息,我建议只使用一个表格视图并手动处理要显示的内容。

    如果您需要更多帮助,请告诉我。

    【讨论】:

      【解决方案3】:

      使用切换条件


      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

          let cell = UITableViewCell()
          switch tableView {
          case firsttableView:
              let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
              cell.label1.text = "Zaid"
              cell.label2.text = "Ahamd"
              return cell
          case secondTableView:
              let cell = tableView.dequeueReusableCell(withIdentifier: "SecondTableViewCell", for: indexPath) as! SecondTableViewCell
              cell.labelText.text = "ZaidAfzalMughal"
              return cell
          default:
              print("Something wrong")
          }
          return cell
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多