问题
I'm new to swift and firebase. Done some research here, could not find solution that would solve my problem, lack of experience.
Got a problem when trying to delete tableView Cell from firebase "Value of type 'Group' has no member 'ref'"
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let ref = groupsArray[indexPath.row].ref
ref!.removeValue()
groupsArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}}
Any ideas what I'm doing wrong?
TAbleView
extension ChatViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groupsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = groupsTableView.dequeueReusableCell(withIdentifier: "groupTableViewCell", for:indexPath) as?
GroupTableViewCell else {return UITableViewCell()}
let group = groupsArray[indexPath.row]
cell.configureCell(title: group.groupTitle, description: group.groupDesc, membersCount: group.membersCount)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let groupFeedViewController = storyboard?.instantiateViewController(withIdentifier: "GroupFeedViewController") as? GroupFeedViewController else {return}
groupFeedViewController.initData(forGroup: groupsArray[indexPath.row])
presentDetail(groupFeedViewController)
}}
Group Class
import Foundation
class Group {
private var _groupTitle: String
private var _groupDesc: String
private var _key: String
private var _memberCount: Int
private var _members: [String]
var groupTitle: String {
return _groupTitle
}
var groupDesc: String {
return _groupDesc
}
var key: String {
return _key
}
var membersCount: Int {
return _memberCount
}
var members: [String] {
return _members
}
init(title: String, description: String, key: String, members: [String], memberCount: Int){
self._groupTitle = title
self._groupDesc = description
self._key = key
self._members = members
self._memberCount = memberCount
}}
回答1:
First, you don't delete a tableViewCell
from Firebase, but you can remove the data from Firebase that you pull to populate your tableView
.
Second, what you're doing wrong is trying to get a class value of ref
from an array.
Third, after deleting the data from the web, you'll still need to remove the cell from the table which your code looks like it will do just fine but I'm putting it here for completeness.
To delete a value from Firebase you need to create a reference and then process things from there. Like this :
let ref = Database.database().reference(fromURL: yourProjectURL)
let groupRef= ref.child("groups").child(groupsArray[indexPath.row].key)
// ^^ this only works if the value is set to the firebase uid, otherwise you need to pull that data from somewhere else.
groupRef.removeValue()
Then delete the item from the array:
groupsArray.remove(at: indexPath.row)
And delete the row from the
tableView
:
tableView.deleteRows(at: [indexPath], with: .automatic)
Everything should look like this :
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let ref = Database.database().reference(fromURL: yourProjectURL)
let groupRef= ref.child("groups").child(groupsArray[indexPath.row].key)
// ^^ this only works if the value is set to the firebase uid, otherwise you need to pull that data from somewhere else.
groupRef.removeValue()
groupsArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
来源:https://stackoverflow.com/questions/49437793/delete-tableview-cell-and-remove-data-from-firebase