gpt4 book ai didi

ios - Firebase 数据未显示在 TableView 中

转载 作者:行者123 更新时间:2023-11-29 00:53:34 27 4
gpt4 key购买 nike

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

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

我将数据放入 Courses 中,然后放入包含 CourseName、AmountOfHoles 和 UpdatedDate 的 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)

}


}

最佳答案

伙计,当找到快照时,你必须插入你的代码!

我想你可以这样走:

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

关于ios - Firebase 数据未显示在 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com