可折叠输入表单 Swift 3 [英] Collapsible input form Swift 3
问题描述
我真的很失望,这就是我问这个问题的原因,我在网上找到了 Collapsible TableView 的教程,但他们用字符串数组或类似的东西填充单元格.
我想用 Swift 3
制作一个这样的手风琴,
几天来,我已经尝试了很多使用 UITableViewController
的东西,因为显然如果你想要不同的单元格,那显然是你唯一可以折叠的东西.
无论如何我开始这样做,但是当我问
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
,
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 UI
s 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)
Here is how you could implement it with UIStackView
:
- Add a vertical
UIStackView
via storyboard - Add a
UIButton
as the first subview within theUIStackView
- Add some
UILabel
s as the second, third... subview within theUIStackView
- Add an
IBAction
for theUIButton
(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 UIStackView
s 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:
这篇关于可折叠输入表单 Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!