LoginSignup
6
5

More than 5 years have passed since last update.

フォーム画面生成ライブラリのFormerを試す

Posted at

UITableViewで設定画面とかを作るのが面倒なので、githubにていつくかピックアップして試したみた。
前回は設定画面生成ライブラリのBohrを試すでBohrを試した。
今回はFormerを試してみる。使用したバージョンは1.4.0。スター数はこれを書いている時点では733。

やりたい事

  • プロフィールとかアカウントの設定等の画面を、簡単に構築したい。
  • 表示するデータはAPIサーバー等やリモートから取得して表示する

ざっくりとまとめ

  • BohrはNSUserDefaultとかに自動で保存したり、設定画面構築って感じが強いが、Formerはネーミングどおりに設定画面用という感じではない。
  • 上記の理由もありNSUserDefault使ってない。
  • ブロック構文を使用して構築してく形ではないので、なにも工夫しないでガシガシ書いていくと、後で見返すとき大変そう。
  • 値が変更された場合に、ハンドリングできる仕組みがある
  • カスタムを想定されているっぽい(未調査)

使い方

インストールはcocoapodsを使用した。インストール後は、使用したいソースでimport FormerすればOK。

TextFieldとかSwitchとかいろいろな行を追加できるが、だいたい以下のような共通の流れだと思う。

  • xxxxxxRowFormerのイニシャライザに渡す引数のブロックで、色とかのプロパティの設定
  • configureのブロックで、フォームに表示したいデータを設定
  • onXXXXX系のブロックで変更のハンドリング
import UIKit
import Former

class SettingViewController: FormViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //-----------------------------------------------------------------
        // セクション1
        //-----------------------------------------------------------------
        let section1 = SectionFormer(rowFormers: [])
        former.append(sectionFormer: section1)

        // ヘッダ
        section1.set(headerViewFormer:
            LabelViewFormer<FormLabelHeaderView>() {
                $0.titleLabel.textAlignment = .Left // ここでプロパティ調整
            }.configure { v in
                v.text = "ヘッダー" // 値はここで設定
            }
        )

        // セルの追加
        section1.add(rowFormers:[
            // スイッチ1
            SwitchRowFormer<FormSwitchCell>() {
                $0.titleLabel.text = "タイトル1"
            }
            .configure { sw in
                sw.switched = false
            },

            // スイッチ2
            SwitchRowFormer<FormSwitchCell>() {
                    $0.switchButton.onTintColor = UIColor.redColor()
                    $0.titleLabel.text = "タイトル2"
                    $0.titleLabel.textColor = UIColor.redColor()
                }.configure { sw in
                    sw.switched = true
                }.onSwitchChanged { value in
           // 変更された場合のハンドリング
                    print(value)
                }
            ]
        )

        //-----------------------------------------------------------------
        // セクション2
        //-----------------------------------------------------------------
        let section2 = SectionFormer(rowFormers: [])
        former.append(sectionFormer: section2)

        // セルの追加
        section2.append(rowFormer:
            // テキストフィールド
            TextFieldRowFormer<FormTextFieldCell>() {
                $0.titleLabel.text = "テキストフィールド"
                $0.textField.textAlignment = .Right
            }.configure { cell in
                cell.text  = "テストテスト"
                cell.placeholder = "プレースホルダ"
            }.onTextChanged { changedText in
                print(changedText)
            }
        )

        // フッター
        section2.set(footerViewFormer:
            LabelViewFormer<FormLabelFooterView>() {
                $0.titleLabel.textAlignment = .Right
            }.configure { v in
                v.text = "フッター"
            }
        )
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

上記のソースで以下のような表示
former.png

フォーム構築後のデータ反映方法

作成した際にxxxxxRowFormerオブジェクトを別の変数で保持しておくか、FormViewControllerのformerプロパティから
取得する。
以下は2つ目のセクションの入力データを更新する例

if let row = self.former.rowFormers[2] as? TextFieldRowFormer<FormTextFieldCell> {
    row.update { cell in
        cell.text = "変更後!!"
    }
}
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5