Reputation: 286
I asked this question How to load more than one cell into UITableView from Firebase Realtime Database when array is saved as a dictionary string yesterday, but had no luck in getting any help.
I decided to try Firestore instead, but I'm getting the same problem, my UITableView is only loading one cell.
I obviously started trying to find where the issue was with the cells and the UITableView but I still haven't been able to with my lack of experience.
I believe there is a simple way to do what I want.
What I want, uses a random user ID, not a Firebase uid.
Please help with suggestions.
This is now the code I use for Firestore.
let userIDSave = defaults.value(forKey: "UUID")
if let savedUserID = userIDSave as? String
{
identifiedUserNumber = "\(savedUserID)"
let user = "USER" + identifiedUserNumber
}
let Name = textField.text!
let Poem = textView.text!
let addPoem = db.collection(user)
addPoem.document(Name).setData([
"Named": Name,
"Poem": Poem
])
I also tried it with the random generated ID
var ref: DocumentReference? = nil
But retrieving seemed more difficult when searching as I want it searchable by either the name or poem, if I can.
db.collection(user).getDocuments()
{ (querySnapshot, err) in
if let err = err
{
print("Error getting documents: \(err)")
}
else
{
for document in querySnapshot!.documents
{
let poemName = document.get("Named") as! String
self.named = poemName
let namedPoem = document.get("Poem") as! String
self.poems = [namedPoem]
print("\(document.documentID) => \(document.data())")
}
}
}
I can still only load one cell in the table.
I can repopulate where I need to PERFECTLY with both the name and the poem when I select the row which has loaded in the table.
The problem is that the table is still only loading the most recent/bottom document, if I put .reversed(), it loads the first one.
Just in case that link from the question yesterday wasn't clicked...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if isSearching
{
return filteredArray.count
}
else
{
return poems.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var array: String?
if isSearching
{
array = filteredArray[indexPath.row]
}
else
{
array = poems[indexPath.row]
}
let cell = mainTable.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as UITableViewCell
cell.textLabel?.text = array
cell.accessoryType = .disclosureIndicator
let indicator = UIActivityIndicatorView()
cell.textLabel?.numberOfLines = 0
return cell
}
The issue is most likely something to do with the UITableView?
Please help me with what I'm missing or what other code you need to see.
Or maybe even with another way to do this?
Thanks in advance.
Upvotes: 0
Views: 65
Reputation: 2950
As it was mentioned in above comment by Paulw11
Your problem is when you say self.poems = [namedPoem]
you are assigning an array with one element and this array replaces any other previously assigned array. The closure is called once for each document, so you need self.poems.append(namedPoem)
. You should probably also store the Firestore document in your array rather than a single string property. Firestore supports Swift Codable So you can map your documents directly to a struct
Upvotes: 2