在for循环的每个循环上创建一个tableView。迅速

时间:2017-07-20 23:09:40

标签: ios swift uitableview

我正在尝试为我的数组中的每个值创建一个tableView,我不知道如何创建它。下面的代码显然不起作用我只是用它来说明我的观点。如何区分tableView协议函数中的表。

let array = ["1","2","3","4","5"]

for value in array{

    let newTableView: UITableView()
    //Continue with creation of table 
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.tableContent.count
}

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

    return cell
}

感谢任何帮助,如果问题不清楚,请发表评论

1 个答案:

答案 0 :(得分:0)

以前,我在一个ViewController中需要多个TableView或集合视图,例如对于不同的标签。

您可以使用标记属性识别每个表。

let data : [Int : [YourObjects]]

for key, value in data {

    let newTableView: UITableView()
    // Identify the table
    newTableView.tag = key
    newTableView.dataSource = self

    //Continue with creation of table 
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.data[tableView.tag].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Dequeue cell
    // Get the data for this table, identified by the tag
    let tableData = data[tableView.tag]
    cell.populateCell(withData: tableData[indexPath.row])
    return cell
}