0

I have table view controller. My problem; The cell in my table view changes every time I enter the view controller. For example; There are 2 data (address-1 and address-2). The first row is address-1, the second row is address-2, when I re-enter the page, the cells change. How can I fix this problem. Thanks

import UIKit import Firebase import MapKit class DenemeTableViewController: UITableViewController { var ref: DatabaseReference! let user: User = Auth.auth().currentUser! var adresListesi = [Adresler]() var adres: Adresler! @IBOutlet var table: UITableView! override func viewDidLoad() { super.viewDidLoad() self.table.delegate = self self.table.dataSource = self Adresdefteri() } override func viewWillAppear(_ animated: Bool) { Adresdefteri() table.reloadData() } override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return adresListesi.count } //prob. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DenemeTableViewCell let adresList = self.adresListesi[indexPath.row] cell.textLabel?.text = adresList.adresname return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:IndexPath) { self.performSegue(withIdentifier: "DENEMEHARİTAGO", sender: indexPath.row) } func Adresdefteri() { ref = Database.database().reference() let adreslerim = ref adreslerim?.child("locations").child(user.emailWithoutSpecialCharacters).child("Adresler").observe( .value) { [self] (snapshot) in if let gelenVeributunu = snapshot.value as? [String:AnyObject]{ self.adresListesi.removeAll() for gelenSatirVerisi in gelenVeributunu { if let sozluk = gelenSatirVerisi.value as? NSDictionary { let key = gelenSatirVerisi.key let adresname = sozluk["adresname"] as? String ?? "" let latitude = sozluk["latitude"] as? Double ?? 0.0 let longitude = sozluk["longitude"] as? Double ?? 0.0 let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) let adres = Adresler(adresname: adresname, latitude: latitude, longitude: longitude, adresid: key) self.adresListesi.append(adres) } } DispatchQueue.main.async { self.table.reloadData() } } } 

tableview

1
  • Your adresListesi is an array that gets its value from snapshot in Database call, which is dictionary and dictionary are unordered, so they append elements in random order. Which is what you see on table. Commented Jun 4, 2021 at 13:10

2 Answers 2

1

Sort the data source array after creating it and reloading the table view, that way it stays consistent. Cheers!

 self.arrayName = arrayName.sorted(by: { $0.valueToSortByInArray > $1.valueToSortByInArray }) 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for answer Sharma and Mishka. Can you show me code on solution, Mishka. Because I tried, but still have problem
Edited the answer, let me know if it works or makes sense!
Arrayname is now coming consistent. But when I go into it, the other data's info comes in.
When I click on the cell, random data comes up. How can I solve this situation @Mishka, any suggestions?
what do you mean? In your didSelectRowAt which gets called when you click on the cell you should be performing a segue.. what are you trying to accomplish?
|
0

You just have to sort the adresListesti to make sure the order is consistent. An example would be:

adresListesi.sort { $0.adresname < $02.adresname } 

Put that right before DispatchQueue.main.async and it should work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.