我正在开发一个简单的UITableview应用程序。第一页是可折叠的UITableView。
以下是各部分的基本代码:
var sections = [
Section(sec: "One",
subSec: ["A", "B", "C", "D", "E"],
expanded: false),
Section(sec: "Two",
subSec: ["F", "G", "H", "I", "J"],
expanded: false),
Section(sec: "Three",
subSec: ["K", "L", "M", "N", "O"],
expanded: false),
Section的结构已在单独的文件中设置:
struct Section {
var sec: String!
var subSec: [String]! // [ ] = Array of Strings
var expanded: Bool!
init(sec: String, subSec: [String], expanded: Bool) {
self.sec = sec
self.subSec = subSec
self.expanded = expanded
}
}
我似乎无法将tableview子部分转换为新的视图控制器并传递数据,即所选字母。
我想要达到的目的是让用户从一个部分中选择一个subSec字母,然后将该字段转换为一个新屏幕,其中包含一组与该字母相关的数据,可能来自字典。
主视图控制器:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate {
@IBOutlet weak var tableView: UITableView!
var sections = [
Section(sec: "One",
subSec: ["A", "B", "C", "D", "E"],
expanded: false),
Section(sec: "Two",
subSec: ["A", "B", "C", "D", "E"],
expanded: false),
Section(sec: "Three",
subSec: ["A", "B", "C", "D", "E"],
expanded: false),
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].subSec.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (sections[indexPath.section].expanded) {
return 44
} else {
return 0
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableHeaderView()
header.customInit(title: sections[section].sec, section: section, delegate: self)
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
cell.textLabel?.text = sections[indexPath.section].subSec[indexPath.row]
return cell
}
func toggleSection(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
tableView.beginUpdates()
for i in 0 ..< sections[section].subSec.count {
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
tableView.endUpdates()
}
}
答案 0 :(得分:0)
使用tableView didSelectRowAtIndexPath:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Subsection \(indexPath.row) in section \(indexPath.section) selected!")
}