入力フォーム用に折りたたみ可能なテーブルビューを作成する必要があります。私はたくさんの検索を行いましたが、2日間は別のコードを試していますが、それらを私が欲しいものに変える方法を知っている。私はいくつかの例を見つけましたが、オブジェクトの配列を表示するためだけのものです。私が必要とするのはこの絵のような形ですが、セルの中に文字列の配列を入れていないので(このチュートリアルのように)、このスクリーンショットと全く同じことをしますが折りたたみ可能なので、 Personal info
それが崩壊します。折りたたみ可能な入力フォームとUITableView Swift 3
これは私がオンラインで見つけたコードの1つですが、私が言ったように、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
}
}
(他にもOKのTableViewで他の方法がある場合)
ありがとうございました。
numberOfRowsの配列数を0に切り替えるだけで、ヘッダーのボタンをクリックすることができます。 –
@ShabbirAhmadヘッダーのボタンをどうやって使うのですか?それを教えてもらえますか? –
viewForHeaderメソッド内にボタンをプログラムで追加します。 –