为什么我的 TableViewController 只返回一个可折叠部分?
Why is my TableViewController only returning a single collapsible section?
我正在使用 UITableViewController
来尝试 return 一系列数据,每个数据都有自己的可折叠部分,类似于下面。
Example Table View Controller
我分别添加了两个单独的 header 部分(“项目 1”)和(“项目 2”),但是只有“项目 2”被 return 编辑并可供 select,而“项目”部分未出现。
这是我用来显示各部分的代码:
var tableViewData = [cellData]()
self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName1 ?? "test?"])]
self.tableViewData = [cellData(opened: false, title: "Item 2", sectionData: [productName2 ?? "test"])]
这是我用来以编程方式更改部分的代码(以更新每个部分下的结果数)。
override func numberOfSections(in tableView: UITableView) -> Int {
return tableViewData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableViewData[section].opened == true {
return tableViewData[section].sectionData.count + 1
} else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var dataIndex = indexPath.row - 1
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].title
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
return cell
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if tableViewData[indexPath.section].opened == true {
tableViewData[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
} else {
tableViewData[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
}
为什么当应用 运行 添加了两个 select 可用部分时,只有一个出现(“项目 1”和“项目 2”?请在下面找到该应用程序的屏幕截图,以直观地了解该页面的当前外观。
In app demonstration image
您实际上并没有添加两个部分 -- 您添加了一个项目,然后将其替换为另一个:
var tableViewData = [cellData]()
self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName1 ?? "test?"])] //this is the first assignment
self.tableViewData = [cellData(opened: false, title: "Item 2", sectionData: [productName2 ?? "test"])] //then, you overwrite it here
相反,您可以这样做:
self.tableViewData =
[cellData(opened: false, title: "Item 1", sectionData: [productName1 ?? "test?"]),
cellData(opened: false, title: "Item 2", sectionData: [productName2 ?? "test"])]
我正在使用 UITableViewController
来尝试 return 一系列数据,每个数据都有自己的可折叠部分,类似于下面。
Example Table View Controller
我分别添加了两个单独的 header 部分(“项目 1”)和(“项目 2”),但是只有“项目 2”被 return 编辑并可供 select,而“项目”部分未出现。
这是我用来显示各部分的代码:
var tableViewData = [cellData]()
self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName1 ?? "test?"])]
self.tableViewData = [cellData(opened: false, title: "Item 2", sectionData: [productName2 ?? "test"])]
这是我用来以编程方式更改部分的代码(以更新每个部分下的结果数)。
override func numberOfSections(in tableView: UITableView) -> Int {
return tableViewData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableViewData[section].opened == true {
return tableViewData[section].sectionData.count + 1
} else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var dataIndex = indexPath.row - 1
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].title
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
return cell
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if tableViewData[indexPath.section].opened == true {
tableViewData[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
} else {
tableViewData[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
}
为什么当应用 运行 添加了两个 select 可用部分时,只有一个出现(“项目 1”和“项目 2”?请在下面找到该应用程序的屏幕截图,以直观地了解该页面的当前外观。
In app demonstration image
您实际上并没有添加两个部分 -- 您添加了一个项目,然后将其替换为另一个:
var tableViewData = [cellData]()
self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName1 ?? "test?"])] //this is the first assignment
self.tableViewData = [cellData(opened: false, title: "Item 2", sectionData: [productName2 ?? "test"])] //then, you overwrite it here
相反,您可以这样做:
self.tableViewData =
[cellData(opened: false, title: "Item 1", sectionData: [productName1 ?? "test?"]),
cellData(opened: false, title: "Item 2", sectionData: [productName2 ?? "test"])]