然后是一个具有动态列swift 4的动态表

问题描述 投票:-1回答:2

表:[{ID,名称,类型,行[{ID,名称}],柱[{ID,名称}]}]

blog.adgager.com/wp-content/uploads/2017/07/mobile_surveys.jpeg

(一个简单的表示)

我需要向一个控制器添加多个表。数据将作为json来。图像将被分类到View控制器中。

我该怎么办?我正在以编程方式使用swift 4 tableview

如何在单个ControllerView中动态创建多个tableview。

该表必须具有多个列。要按表类型填充的表行。如果行类型是复选框,则第一列中的标签将是所有其他列中的复选框。

我该怎么办?

感谢从现在开始。

swift dynamically-generated
2个回答
0
投票

我建议您采用一个包含多个节和行的表格视图。不确定您的UX外观如何。但是,当我们有numberOfSection和NumberOf Rows的概念时,其中每个节都可以由标题(分组表视图)分隔开,然后不知道为什么在一个ViewController中需要多个表视图。


0
投票

创建多个表视图相对简单。我假设CollectionView同样适用,如果您想使用真正的多列而不是mukti组件的tableView单元格,这就是您所需要的,尽管我从未尝试过。

简而言之,您需要实例化两个表视图,然后将它们添加为子视图。有趣的一点是处理委托函数。您有两种选择:

  1. 使用两个不同的类作为代表,每个代表一个。这通常更容易理解,但确实涉及很多样板代码的重复。
  2. 对两个委托都使用1类(甚至可能是视图控制器,但是它冒着巨大的视图控制器综合症的风险,并在每种方法中测试它也适用于哪种表视图。

其中第一个很明显,因此下面是第二个的高级示例。

class MyCustomCell: UITableViewCell {
  static let cellID = "CustomCellID"
  // set up custom tableViewCell as required
}

class MyVC: UIViewController {
  var table1 = UITableView()
  var table2 = UITableView()

  override func viewDidLoad() {
    super.viewDidLoad
    table1.delegate = self
    table1.datasource = self
    table2.delegate = self
    table2.datasource = self
    view.addSubview(table1)
    view.addSubview(table2)
    tableView.register(MyCustomCell.self, forCellReuseIdentifier: MyCustomCell.cellID)
    tableView.register(MyCustomCell.self, forCellReuseIdentifier: MyCustomCell.cellID)
    //some AutoLayout code to layout the tables in the view
  }
}

extension MyVC: UITableViewDelegate, UITableViewDataSource {
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     if tableView === table1 {
        //return appropriate value for section in table 1
     } else {
       //return appropriate value for section in table 1
     }
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView === table1 {
      //create and return cell for table 1
    } else {
      //create and return cell for table 2
    }
  }
}

显然,这只是概述,将需要实际的实现代码和其他必要的tableView函数。注意使用===检查tableViews。这将检查它们是完全相同的对象,而不仅仅是等效的对象。

如果您需要两个表都具有相同的位置(例如,您希望它们使用相同的tableViewCell,那么您可能无需检查就可以删除。

已经写完了所有这些,除非至少有一个表显示的是静态行数并且可以禁用滚动,否则我想说它在iPhone上看起来很糟糕,在大多数情况下,在iPad上并不太好!

© www.soinside.com 2019 - 2024. All rights reserved.