Problem : CollectionView Repeat Same Item For All Cells

Hi,

I am loading data from Firebase:

1 - I pass a string as key to “getExpenseIds” , this key have one or more keys as child.

func getExpenseIds(completion:@escaping( [String: Any])->Void)
            { 
                 let expensexId =  Database.database().reference(withPath: "expensesKyes").child("\(expensesMasterKey)")
                expensexId.observe( .value, with: { (snapshot) in

                    guard let dictionaries = snapshot.value as? [String: Any] else
                           {
                                return
                           }
                             completion(dictionaries)

                             }, withCancel: nil)
            }

retrieved keys from Firebase :

{
 {
    "c7952f38-7c34-4e50-876b-10a49ad7646d" : false,
    "dec69e80-ed07-4bbe-a464-56bad6fd731a" : false
  },
..
}

2 - After I recieve keys, I then the run them through a loop that call the data for each key from another node as you can see in “getExpenses” .

func getExpenses(completion:@escaping()->Void)
   {

        let expense = Expense()
            getexpensesIds { (ids) in
            for i in ids
       {


    self.expensesRef.child(i.key).observe(.value, with: {(snapshot) in     
           let master = snapshot.value
            guard let dictionary = master as? [String:Any] else
           {
               return
           }

           if let exDLT = dictionary["isDlt"]
           {
            expense.isDlt = exDLT as! Int
            }
             if expense.isDlt == 0
            {
               if let expenseuid = dictionary["expUID"]
            {
                expense.expenseUID =  expenseuid as! String
            }
                expense.amount = dictionary["expensePrice"] as! Double
                expense.expensesDetails = dictionary["expenseDiscription"] as! String
                expense.expensesSegment = dictionary["expenseTilte"] as! String

                self.arrExpensess.append(expense)

             }

                })

                       }
             completion()

                  }
   }

Data retrieved from Firebase :

 {
      "115eb1d9-330a-4428-b292-09714bae2323" : {
        "expenseDiscription" : "XXXX ",
        "expensePrice" : 0,
        "expenseTilte" : "Grid Fin",
        "isDlt" : false,
        "xpnUID" : "115eb1d9-330a-4428-b292-09714bae2323"
      },
      "18e74f31-dae8-4037-b70b-2272af83bad6" : {
        "expenseDiscription" : "XXXX ",
        "expensePrice" : 0,
        "expenseTilte" : "Engine",
        "isDlt" : false,
        "xpnUID" : "18e74f31-dae8-4037-b70b-2272af83bad6"
      },
    .
    .
    .
    }

When the data is back , the data is populated in CollectionView.

I make the call for Firebase in ViewWillAppear() by calling a function that passes a completion handler, In the closure I call collectionView.reloadData() as follows :

    override func viewWillAppear(_ animated: Bool){
   super.viewWillAppear(animated)

            self.getExpenses{
                self.collectionView?.reloadData()
            }
}

I can’t seem to Identify the root cause but in general, when data is retrieved and before populate data , it shows the wright data in an array, after populating , it repeats only one item for all cells.

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid, for: indexPath) as! ExpensesCell

     cell.expenseItem =   self.arrExpensess[indexPath.item]
        return cell
}

in the following example , I have two different data items, but it shows only the same item :

UICollecionView after populating data

The array items count is correct, but the array items are all the same.

I hope that I explained it clearly, I need the collectionview to fill the wright data for each cell.

Hey,

I am not 100% sure I got your problem, but are you sure that what data you receive is the one that you expect ? I know this is a dummy question, but I never had problems with reloading data as long as the data and/or the index paths are correct.

You said something like this “retrieved and before populate data”, what does populate refer to? Do you mean reloading the collection view ? Are you sure you update the ‘data source array’ of the collection view before calling the reloadData() method ?

Hi,

Thanks replying. I mean reloading data in collectionView, and yes I am sure that the data source array is updated before calling the reloadData() method .

I don’t have another idea unless a see what you are doing there. Could provide a little snippet from your code to see exactly the context?

@ahmedsabir Do you still have issues with this?

This topic was automatically closed after 166 days. New replies are no longer allowed.