你好我是新来的SO我有这种类型的多维数组
[["name1","name2"],["name3,"name4"],["name5","name6"]]
我想创建一个包含多个部分的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
}
}
以下是我的代码目前的样子,但每个部分都显示相同的数据
答案 0 :(得分:1)
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)
也许allGroups没有任何成员,因此allGroups [0]会因超出范围问题而崩溃。
答案 1 :(得分: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 data[section].count
}
要调整您提供的代码,您应该只需要这样做
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allGroups[section].count
}
答案 2 :(得分:-1)
我认为您只需要更改代码的这一部分:
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
}