Arrays 如何在固定数量的节中有不同的行?

Arrays 如何在固定数量的节中有不同的行?,arrays,swift,xcode,uitableview,Arrays,Swift,Xcode,Uitableview,我遇到了问题,无法理解如何在TableViewController的每个部分中都有不同数量的行。此外,您能告诉我如何访问它们吗 如果我必须设置array.count,我不明白它应该如何工作 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 为此,您可以使用二维数组,如下所示: var array = [[MyType]]() override func

我遇到了问题,无法理解如何在TableViewController的每个部分中都有不同数量的行。此外,您能告诉我如何访问它们吗

如果我必须设置array.count,我不明白它应该如何工作

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

为此,您可以使用
二维
数组,如下所示:

var array = [[MyType]]()
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    array[section].count
}

假设单元格的模型只是一个int。您可以将表视图的模型设置为数组的数组。使用外部数组保存节,使用内部数组保存节的行:

let model: [[Int]] = [
    [1, 2, 3],              //First section, 3 rows
    [4],                    //2nd section, 1 row
    [5, 6, 7, 8],           //3rd section, 4 rows
    [9, 10, 11, 12, 13, 14] //4th section, 6 rows
]
正如弗兰肯斯坦在回答中所说:

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

override func numberOfSections( in tableView: UITableView) -> Int {
    return model.count
}
cellForRowAt函数将节号用作外部数组的索引,行用作内部数组的索引:

let item = model[section][row]

在数组中组织数据。主阵列每个部分有一个子阵列,子阵列有您的单元格信息。