可折叠输入表单 Swift 3 [英] Collapsible input form Swift 3

查看:27
本文介绍了可折叠输入表单 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 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)

解决方案

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:

这篇关于可折叠输入表单 Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆