2017-08-09 21 views
0

入力フォーム用に折りたたみ可能なテーブルビューを作成する必要があります。私はたくさんの検索を行いましたが、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で他の方法がある場合)

ありがとうございました。

this

+0

numberOfRowsの配列数を0に切り替えるだけで、ヘッダーのボタンをクリックすることができます。 –

+0

@ShabbirAhmadヘッダーのボタンをどうやって使うのですか?それを教えてもらえますか? –

+0

viewForHeaderメソッド内にボタンをプログラムで追加します。 –

答えて

1

ヘッダーがクリック可能(ボタン使用ViewForHeaderデリゲートメソッドを追加)し、絵コンテから一つのセル(ストーリーボードで設計、実装)を作成し、名前、lastNameのためにコンセントを取り、都市などとセクションごとにタイトルを変更することはできセクション( (行の高さを動的に変更する)を閉じると開くようにします。

+0

ありがとうございます、これは単なる例です、フォームのtextFieldsではなく、スイッチとチェックボックスとImagePicker ... –

+1

その場合、すべてのセルが同じ場合、1つのセルを設計して再利用するか、またはあなたは何か他のことを試す必要があります –

関連する問題