【发布时间】:2017-08-09 06:59:41
【问题描述】:
我需要为输入表单制作一个可折叠的 tableView,我已经搜索了很多,两天来我正在尝试不同的代码,但它们不符合我的需要,我不知道如何将它们更改为我想要的是。我找到了几个示例,但它们仅用于显示对象数组。我需要的是像这张图片一样的表格,但不是用字符串数组填充单元格(就像我发现的许多教程一样),而是制作与此屏幕截图完全相同但可折叠的东西,以便例如当我点击时Personal info 它崩溃了。
这是我在网上找到的代码之一,但正如我所说,我不想用 Array 填充它。我只是想用不同的单元格添加尽可能多的部分,并简单地使它们可折叠。
(顺便说一句,我的部分显示已折叠,但当我点击每个应用程序崩溃时)
TestVC.swift
class TestVC: UITableViewController, ExpandableHeaderViewDelegate {
var sections = [
Section(genre: "Animation", expanded: false),
Section(genre: "Horror", expanded: false)
]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 4
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (sections[indexPath.section].expanded){
return 44
} else{
return 0
}
}
// Just to make it look nice
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableHeaderView()
header.customInit(title: sections[section].genre, section: section, delegate: self)
return header
}
func toggleSection(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
tableView.beginUpdates()
for i in 0..<6 {
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
tableView.endUpdates()
}
}
ExpandableHeaderView.swift
protocol ExpandableHeaderViewDelegate {
func toggleSection(header: ExpandableHeaderView, section: Int)
}
class ExpandableHeaderView: UITableViewHeaderFooterView {
var delegate: ExpandableHeaderViewDelegate?
var section: Int!
override init(reuseIdentifier: String?){
super.init(reuseIdentifier: reuseIdentifier)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderAction)))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer){
let cell = gestureRecognizer.view as! ExpandableHeaderView
delegate?.toggleSection(header: self, section: cell.section)
}
func customInit(title: String, section: Int, delegate: ExpandableHeaderViewDelegate){
self.textLabel?.text = title
self.section = section
self.delegate = delegate
}
override func layoutSubviews() {
super.layoutSubviews()
self.textLabel?.textColor = UIColor.white
self.contentView.backgroundColor = UIColor.darkGray
}
}
(如果有其他方法可以使用 TableView 也可以)
提前感谢您的帮助。
【问题讨论】:
-
您可以在标题上单击按钮,然后单击标题后只需将 numberOfRows 数组计数切换为 0。
-
@ShabbirAhmad 我如何在标题上按按钮?你能告诉我吗?
-
在 viewForHeader 方法中以编程方式添加按钮
-
@AbedNaseri 在标题
UIView中添加tap gesture而不是button你将return -
@AbedNaseri 你可以使用其中任何一个,但如果它像家庭控制器或主控制器,那么 Tableviewcontroller 很好。
标签: ios uitableview swift3