6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Eurekaで非同期処理で取得したデータでフォームを更新して画面に反映させる

Posted at

方法

非同期処理のコールバック関数内に以下を書く。

self.form.allRows.forEach({$0.updateCell(); $0.reload()})

import Eureka

final class ExampleFormViewController: FormViewController {

    private var user: User?

    ...
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setView()
        setForm()
        fetchUser()
    }

    private func setView() {
        ...
    }

    private func setForm() {
        self.form
           +++ Section("User info")
           <<< TextRow() { row in
                row.title = "Name"
                row.placeholder = "hoge fuga"
            }.cellUpdate { [unowned self] cell, row in
                // フェッチしたuserのnameを代入
                row.value = self.user?.name
            }
            ...
    }

    private func fetchUser() {
        let successHandler: (User) -> Void = { user in
           self.user = user
           // フォームを更新・画面に反映
           self.form.allRows.forEach({$0.updateCell(); $0.reload()})
        }

        UserLogic.get(successHandler: successHandler, failureHandler: {_ in})
    }
}

説明

$0.updateCell()RowcellUpdateが呼ばれるので、ここでrow.valueを更新する。
$0.reload()Rowの変更を画面に反映させる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?