PickerInlineRow,PickerRow - 填充选项的最佳实践

时间:2016-11-23 14:03:02

标签: ios firebase firebase-realtime-database

iOS的新功能。我正在使用Firebase作为后端数据,我想知道为PickerInlineRow等内容填充选项列表的推荐方法。我通常做的是以下几点;

  • 创建变量以保存表单中使用的数据
  • 致电Firebase以检索数据
  • 将Firebase中的值加载到我的本地变量
  • 在Firebase调用的闭包中,加载表单
  • 在表单中,使用我的变量填充值
  • 使用.onchange事件更新变量

当用户保存时,变量用于更新数据库。这一切都有效,但是在尝试填充表单中的下拉列表时会出现问题。我知道如何设置选择器的选项,但不知道如何构造序列,以便在使用之前填充我用于选项的数组。如果我将选项设置为数组,但数组尚未完成填充,则选择器没有值。

协调这些事件的推荐方法是什么?我在下面粘贴了一个示例Eureka表格。

import UIKit
import Firebase
import GeoFire
import Eureka

class QuestDetailsViewController: FormViewController {

let ref: FIRDatabaseReference = FIRDatabase.database().reference()

var key = String()
var isNew = Bool()

var locationKeys = [String]()
var locationNames = [String]()

var locationKey: String?
var locationName: String?
var startDate: Date?
var endDate: Date?

override func viewDidLoad() {
    super.viewDidLoad()

    loadData()
}

func loadData(){
    // load lookup values
    if isNew == false {
        ref.child("Quests").child(key).observeSingleEvent(of: .value, with: {(snapshot) in
            if let item = snapshot.value as? [String:AnyObject]{
                self.locationName = item["LocationName"] as? String
                self.locationKey = item["LocationKey"] as? String
                self.startDate = DateFromInterval(interval: (item["StartDate"] as? Double)!)
                self.endDate = DateFromInterval(interval: (item["EndDate"] as? Double)!)
            }
            self.loadDropdowns()

        } , withCancel: {error in
            print("Error : \(error.localizedDescription)")
        })
    }
    else {
        self.loadDropdowns()
    }

}

func loadDropdowns() {
    ref.child("Places").queryOrdered(byChild: "PlaceName").observeSingleEvent(of: .value, with: {(snapshot) in

        for item in (snapshot.children.allObjects as? [FIRDataSnapshot])! {
            let thisPlace = item.value as! [String: AnyObject]
            self.locationKeys.append(item.key)
            self.locationNames.append(thisPlace["PlaceName"] as! String)
        }

        self.loadForm()

    }, withCancel: {error in

    })
}

func loadForm() {

    form +++ PickerInlineRow<String>() {
        $0.tag = "locationPicker"
        $0.title = "Location"
        $0.options = locationNames
        $0.value = self.locationName
    }.onChange({ (row) in
        self.locationName = row.value
        let itemIndex = self.locationNames.index(of: self.locationName!)
        self.locationKey = self.locationKeys[itemIndex!]
    })

        <<< DateTimeRow() {
            $0.tag = "startDate"
            $0.title = "From"
            $0.value = self.startDate
    }.onChange({ (row) in
        self.startDate = row.value
    })

        <<< DateTimeRow() {
            $0.tag = "endDate"
            $0.title = "To"
            $0.value = self.endDate
    }.onChange({ (row) in
        self.endDate = row.value
    })

        +++ ButtonRow() {
            $0.title = "Challenges"
            $0.presentationMode = PresentationMode.segueName(segueName: "segueChallenges", onDismiss: nil)
    }
        +++ ButtonRow() {
            $0.title = "Save Changes"
    }.onCellSelection({ (cell, row) in
        self.saveChanges()
    })
}

func saveChanges() {
    let childUpdates = ["LocationKey": locationKey!, "LocationName": locationName!, "StartDate": IntervalFromDate(date: startDate!), "EndDate": IntervalFromDate(date: endDate!)] as [String : Any]
    if isNew == true {
        key = ref.child("Quests").childByAutoId().key
    }
    ref.child("Quests").child(key).updateChildValues(childUpdates, withCompletionBlock: {(error, ref) in
        self.navigationController?.popViewController(animated: true)
    })
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueChallenges" {
        let vc = segue.destination as? ChallengesTableViewController
        vc?.questKey = key
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

1 个答案:

答案 0 :(得分:0)

您要加载没有任何值的表单,然后当值从firebase返回时,您可以使用行标签重新加载行

//Completion handler of Firebase {
      if let row = self.form.rowBy(tag: "rowTag") as? PickerInlineRow<RowType> {
        row.reload()
      }
}