Ios 如何创建多节表视图
你好,我是新来的,所以我有这种多维数组Ios 如何创建多节表视图,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,你好,我是新来的,所以我有这种多维数组 [[“名称1”、“名称2”]、[“名称3”、“名称4”]、[“名称5”、“名称6”] 我想创建一个包含多个部分的UITableview let tableView: UITableView = { let table = UITableView() return table }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor
[[“名称1”、“名称2”]、[“名称3”、“名称4”]、[“名称5”、“名称6”]
我想创建一个包含多个部分的UITableview
let tableView: UITableView = {
let table = UITableView()
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
tableView.delegate = self
tableView.dataSource = self
tableView.register(TableCell.self, forCellReuseIdentifier: "tablecell")
view.addSubview(tableView)
view.addConstraintsWithFormat("H:|[v0]|", views: tableView)
view.addConstraintsWithFormat("V:|[v0]|", views: tableView)
}
override func viewDidAppear(_ animated: Bool) {
print("Groups are \(allGroups.count)")
print(allGroups[0].count)
}
func numberOfSections(in tableView: UITableView) -> Int {
return allGroups.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allGroups[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell
for i in allGroups{
cell.textLabel?.text = i[indexPath.row]
for j in i{
print(j)
}
}
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let vw = UIView()
vw.backgroundColor = UIColor.gray
return vw
}
}
下面是我的代码当前的样子,但每个部分都显示相同的数据
func numberOfSections(in tableView: UITableView) -> Int {
return allGroups.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allGroups[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell
cell.textLabel?.text = allGroups[indexPath.section][indexPath.row]
return cell
}
注意
你不应该这样做
print(allGroups[0].count)
可能所有组都没有任何成员,因此所有组[0]都会因超出范围问题而崩溃。您可能希望将数据保存在变量中
var data = [["name1","name2"],["name3","name4"],["name5","name6"]]
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
从每个部分获取数据(未经测试)
要调整您提供的代码,您只需执行以下操作
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allGroups[section].count
}
我认为您只需要更改代码的这一部分:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell
for i in allGroups{
cell.textLabel?.text = i[indexPath.row]
for j in i{
print(j)
}
}
return cell
}
像这样的东西
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell
let group = allGroups[indexPath.section] as! [String]
let cellText = group[indexPath.row]
cell.textLabel?.text = cellText
return cell
}
到目前为止,你做了什么?向我们展示你的代码:-)我被卡住了,我不知道如何访问数组来为我的代表使用它,比如我无法计算
numberOfRowsInSection
或numberOfSections
@Johnmayowa使用你当前正在尝试的代码编辑你的问题,这里已经回答了问题:你为什么要实现titleForHeaderInSection
和viewForHeaderInSection
?只能实现其中的0或1,不能同时实现。如何从我使用的数组中获取节数?请查看示例数据。[[“name1”、“name2”]、[“name3”、“name4”]、[“name5”、“name6”]
。你认为有多少节?我认为OP可能需要帮助,根据他的样本数据集,如何动态确定节的数量,这可能会增加或减少。有三个节可以让数组变成[“组”:[“name1”,“name2”],“组”:[“name3”,“name4”],“组”:[“name5”,“name6”]]
numberOfRowsInSection如何?节和行从0开始。谢谢,我想是这样的,但无法确定。我将删除最后一位。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell") as! TableCell
let group = allGroups[indexPath.section] as! [String]
let cellText = group[indexPath.row]
cell.textLabel?.text = cellText
return cell
}