每当我向Firebase添加新数据或更新Firebase控制台中的数据时,我的tableView
数据都会重复。我所遵循的udemy教程从未展示过如何解决这个问题。 Google search提出了我的问题,但似乎没有解释问题的根源;我追问问题的根源而不仅仅是答案。我正在学习,但这会让我感到烦恼。
一个答案提到self.members.removeAll()
的使用,但我使用的是Swift 3,在哪里放这个以及为什么?
我的问题与this有关,但答案似乎含糊不清,而不是真正的答案。我有一个configureCell()
,但应该添加什么?
当有人解释并且只是添加答案时,我会回答我问题的大部分答案。请问发生了什么?为什么?我是否需要重新加载/删除内容:
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell{...}
编辑:
var expenses = [Expense]()
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return expenses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let expense = expenses[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "expenseFeedCell") as? ExpenseFeedCell {
cell.selectionStyle = .none
cell.configureCell(expense: expense)
return cell
} else {
return ExpenseFeedCell()
}
}
和费用类:
class Expense {
// Private variables
private var _date: Double!
private var _type: String!
private var _amount: Double!
private var _notes: String!
private var _expenseId: String!
// Setting up the getteres
var type: String {
return _type
}
var date: Double {
return _date
}
var amount: Double {
return _amount
}
var notes: String {
return _notes
}
var expenseId: String {
return _expenseId
}
init(type: String, date: Double, amount: Double, notes: String) {
self._type = type
self._date = date
self._amount = amount
self._notes = notes
}
init(expenseId: String, expenseData: [String: AnyObject]) {
self._expenseId = expenseId
if let type = expenseData["type"] as? String {
self._type = type
}
if let date = expenseData["date"] as? Double {
self._date = date
}
if let amount = expenseData["amount"] as? Double {
self._amount = amount
}
if let notes = expenseData["notes"] as? String {
self._notes = notes
}
}
}
答案 0 :(得分:0)
如果每次有新条目时都会调用用于读取数据的firebase方法,则应在添加数据之前删除数据内容。
所以在firebase方法中,首先要有一个空数组。之后,您可以使用snapchot处理并填充相同的数组。
添加self.tableView.reloadData()
以使用此数组中的新数据重新加载视图