【问题标题】:Swift Table View Cell Changing DataSwift 表格视图单元格更改数据
【发布时间】:2021-08-22 12:58:47
【问题描述】:

我有表格视图控制器。我的问题;每次我进入视图控制器时,表格视图中的单元格都会发生变化。例如;有 2 个数据(地址 1 和地址 2)。第一行是地址1,第二行是地址2,当我重新进入页面时,单元格会发生变化。我该如何解决这个问题。谢谢

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

【问题讨论】:

  • 您的 adresListesi 是一个数组,它从数据库调用中的快照中获取其值,即字典和字典是无序的,因此它们以随机顺序附加元素。这是你在桌子上看到的。

标签: ios swift firebase firebase-realtime-database tableview


【解决方案1】:

在创建数据源数组并重新加载表视图后对其进行排序,以保持一致。干杯!

    self.arrayName = arrayName.sorted(by: { $0.valueToSortByInArray > $1.valueToSortByInArray })

【讨论】:

  • 感谢夏尔马和米什卡的回​​答。你能告诉我解决方案的代码吗,Mishka。因为我试过了,还是有问题
  • 编辑了答案,让我知道它是否有效或有意义!
  • Arrayname 现在变得一致了。但是当我进去的时候,其他数据的信息进来了。
  • 当我点击单元格时,随机数据出现。我该如何解决这种情况@Mishka,有什么建议吗?
  • 什么意思?在您单击单元格时调用的 didSelectRowAt 中,您应该执行 segue ..您要完成什么?
【解决方案2】:

您只需对 adresListesti 进行排序以确保顺序一致。 一个例子是:

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

把它放在DispatchQueue.main.async 之前,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多