【问题标题】:Firebase Data not showing in TableViewFirebase 数据未显示在 TableView 中
【发布时间】:2016-06-16 08:39:19
【问题描述】:

所以基本上我想从 Firebase 获取数据并将其放在 tableView 中,然后当一个单元格被删除时,我想从 firebase 和 table view 中删除数据......但是使用我的代码,数据甚至不是出现在 TableView 中,我真的不知道出了什么问题......?

如果有帮助,这是我的数据库结构: Structure

我将数据放入 Courses 中,然后放入包含 CourseName、AmountOfHoles 和 AdditionalDate 的 childByAutoId,然后将其返回到快照中,将快照存储在名为 course 的数组中,然后在 cellForRowAtIndexPath 中获取单元格的变量,但不知何故单元格是没有显示在 tableView 上...然后我会删除 commitEditingStyle 中的单元格和数据,但它甚至没有到达那个,因为单元格没有显示...

我是 StackOverflow 的新手,所以如果有什么看起来愚蠢或错误的地方,请见谅......不要费心告诉我......

class CoursesViewController: UITableViewController {

  var ref = FIRDatabaseReference.init()
  override func viewDidLoad() {

  ref = FIRDatabase.database().reference()

  tableView.allowsMultipleSelectionDuringEditing = false


  //let a = ref.childByAutoId()
  //a.setValue("hi")

  }

  var courses: [FIRDataSnapshot]! = []

  override func viewDidAppear(animated: Bool) {
  super.viewDidAppear(animated)

  let CoursesRef = ref.child("Courses")

  CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshpt in

  self.courses.append(snapshpt)


  })


  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return self.courses.count
  }


  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
  UITableViewCell {

  let cell: UITableViewCell! = self.tableView .dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath)


  let courseSnap: FIRDataSnapshot! = self.courses[indexPath.row]

  let course = courseSnap.value

  let coursename = course?.objectForKey("CourseName") as! String
  let amountofholes = course?.objectForKey("AmountOfHoles") as! String
  let addeddate = course?.objectForKey("AddedDate") as! String

  cell.textLabel?.text = coursename + " " + amountofholes + " Holes"
  cell.detailTextLabel?.text = addeddate

  return cell

  }

  override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  return true
  }

  override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  if editingStyle == .Delete {

  // Find the snapshot and remove the value
  let courseitem = courses[indexPath.row]

  courseitem.ref.removeValue()


  }
  }

  @IBAction func addButtonDidTouch(sender: AnyObject) {


  // Alert View for input

  let alert = UIAlertController(title: "Course Item",message: "Add Course",preferredStyle: .Alert)

  let saveAction = UIAlertAction(title: "Save", style: .Default) { (action: UIAlertAction) -> Void in

  //Get Date String
  let date = NSDate()
  print(date)

  let dateFormatter = NSDateFormatter()
  dateFormatter.dateFormat = "dd-MM-yyyy 'at' HH:mm"
  let dateString = dateFormatter.stringFromDate(date)
  print(dateString)
  //
  let courseField = alert.textFields![0]
  let holesField = alert.textFields![1]





  let Courses = self.ref.child("Courses").childByAutoId()

  let course = ["AddedDate": dateString as AnyObject,
  "CourseName": courseField.text as! AnyObject,
  "AmountOfHoles": holesField.text as! AnyObject]

  Courses.setValue(course)


  }


  //Cancel
  let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (action: UIAlertAction) -> Void in
  }

  //TextField placeholder in alert
  alert.addTextFieldWithConfigurationHandler {
  (courseField: UITextField!) -> Void in

  courseField.placeholder = "Course Name"
  }
  alert.addTextFieldWithConfigurationHandler {
  (holesField: UITextField!) -> Void in

  holesField.placeholder = "Holes (6/9/18)"
  }

  //Add alert
  alert.addAction(saveAction)
  alert.addAction(cancelAction)

  presentViewController(alert, animated: true, completion: nil)

  }


}

【问题讨论】:

  • 重新加载你的tableview ...
  • 哇,我现在感觉很愚蠢...谢谢...这可行,但现在如果我更改视图并再次返回单元格重复...如果我删除它就不会走开……?
  • 在 viewDidLoad 中添加您的 viewDidAppear 代码并检查
  • 那行得通……现在它们不会重复,但如果我删除它们,它们不会在 tableview 中消失,但它们会在 firebase 中消失
  • 不,它不会消失,留在 tableview 中

标签: ios swift uitableview firebase firebase-realtime-database


【解决方案1】:

发现快照后,您必须插入代码!

我认为你可以这样:

CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in
  self.courses.append(snapshot)
  self.yourTable.insertRowsAtIndexPaths([NSIndexPath(forRow: self.courses.count-1, inSection: 0)], withRowAnimation: .Bottom)
          })

【讨论】:

  • ..我通过重新加载表格视图并将 observeventtype 放入 viewdidload() 解决了这个问题......但我在某处找到了这个,感谢
【解决方案2】:

检查此代码

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.courses.count > 0){
 tablecount = self.courses.count
} else {

 tablecount = 0
 self.table.reloaddata()
}
return tablecount
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2017-11-13
    • 2018-04-07
    • 2017-01-06
    • 2019-10-08
    • 1970-01-01
    相关资源
    最近更新 更多