Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// FirebaseDataTableViewController.swift
// MyFootPrints
//
// Created by Tetsuya Shiraishi on 2017/04/11.
// Copyright © 2017年 MUSHIKAGO DESIGN STUDIO CO., LTD. All rights reserved.
//
import UIKit
import Firebase
class FirebaseDataTableViewController: UITableViewController {
let kUserPath = "user"
let kDocumentsPath = "documents"
let kLabelString = "label"
var iRootRef:FIRDatabaseReference!
var iThisRef:FIRDatabaseReference?
var iTableData:[FIRDataSnapshot] = [FIRDataSnapshot]()
@IBAction func addItemButtonAction(_ sender: UIBarButtonItem) {
if let _ = FIRAuth.auth()?.currentUser {
let alert = UIAlertController(title: "新規追加", message: "追加するアイテムのラベル名を入力してください。", preferredStyle: .alert)
alert.addTextField(configurationHandler: { (textField:UITextField) -> Void in
// textField.text = "default text."
textField.placeholder = "ラベル名"
})
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields![0] as UITextField
print("Text field: \(textField.text)")
let str:String = textField.text!
let _data : Dictionary<String, AnyObject> = [
self.kLabelString: str as AnyObject
]
let newChild = self.iThisRef?.childByAutoId()
newChild?.setValue(_data)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action:UIAlertAction) -> Void in
print("Text field: cancel")
}))
self.present(alert, animated: true, completion: nil)
} else {
print("No user is signed in.")
}
}
override func viewDidLoad() {
super.viewDidLoad()
iRootRef = FIRDatabase.database().reference()
observeData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return iTableData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let label:FIRDataSnapshot = self.iTableData[indexPath.row].childSnapshot(forPath: self.kLabelString)
if label.exists() {
cell.textLabel?.text = label.value as? String
}else{
cell.textLabel?.text = self.iTableData[indexPath.row].key
}
return cell
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
//tableView.deleteRows(at: [indexPath], with: .fade)
self.iThisRef?.child(self.iTableData[indexPath.row].key).removeValue()
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
func observeData(){
if let user = FIRAuth.auth()?.currentUser {
self.iThisRef = iRootRef.child(self.kUserPath).child(user.uid).child(self.kDocumentsPath)
self.iThisRef?.observe(FIRDataEventType.value, with: { (snapshot:FIRDataSnapshot) in
var tempArray = [FIRDataSnapshot]()
for item in snapshot.children {
let child = item as! FIRDataSnapshot
tempArray.append(child)
}
self.iTableData = tempArray
self.tableView.reloadData()
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.