Collapsible input form Swift 3

2019-09-17 15:43发布

I am really getting disappointed that's why I ask this question, I found tutorials online for Collapsible TableView but they are with populating the cells with an array of Strings or something similar.

I want to make an Accordion like this with Swift 3,

enter image description here

for some days already I tried a lot of things with UITableViewController because apparently that's the only thing you can make collapsible if you want different cells.

Anyway I started to do it but as I asked my question here I cannot show different UIs in each Cell of each Section.

I am sure there's a way to do it, anyone has any suggestions how to make this work?

I thought maybe there's another way to do it (e.g. with ScrollView or something)

标签: ios swift swift3
1条回答
Summer. ? 凉城
2楼-- · 2019-09-17 16:22

Here is how you could implement it with UIStackView:

  1. Add a vertical UIStackView via storyboard
  2. Add a UIButton as the first subview within the UIStackView
  3. Add some UILabels as the second, third... subview within the UIStackView
  4. Add an IBAction for the UIButton (the first subview)

Implement the IBAction like this:

@IBAction func sectionHeaderTapped(sender: UIButton) {
    guard let stackView = sender.superview as? UIStackView else { return }
    guard stackView.arrangedSubviews.count > 1 else { return }
    let sectionIsCollapsed = stackView.arrangedSubviews[1].hidden

    UIView.animateWithDuration(0.25) { 
        for i in 1..<stackView.arrangedSubviews.count {
            stackView.arrangedSubviews[i].hidden = !sectionIsCollapsed
        }
    }
}

Create multiple UIStackViews like this (you can always use the same IBAction for the UIButton) and embed all of them in a parent (vertical) UIStackView to create a view like in your screenshot.

Feel free to ask if anything is unclear.

Result:

result

查看更多
登录 后发表回答