【发布时间】:2017-01-05 12:38:27
【问题描述】:
每当我向 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
}
}
}
【问题讨论】:
标签: swift xcode firebase swift3 xcode8