You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术求助:Firestore数据无法在TableView中正常显示

Fixing Firestore Data Display in TableView

First off, I spot a quick typo in your Sourse extension that’s definitely causing issues—your guard statement cuts off at "conte..." instead of "content". Let’s fix that first:

extension Sourse : DocumentSerializeable { 
    init?(dictionary: [String : Any]) { 
        guard let name = dictionary["name"] as? String, 
              let content = dictionary["content"] as? String, // Fixed the typo here
              let timeStamp = dictionary["timestamp"] as? Date else { // Ensure timestamp is cast correctly
            return nil 
        } 
        self.name = name 
        self.content = content 
        self.timeStamp = timeStamp 
    } 
}

Next, let’s make sure you’re actually fetching the data from Firestore properly and updating your TableView. Firestore calls are asynchronous, so you need to reload the TableView on the main thread once the data is retrieved. Here’s an example of how to set that up:

// Declare an array to hold your data
var sources = [Sourse]()

// Function to fetch data from Firestore
func fetchData() {
    let db = Firestore.firestore()
    db.collection("your-collection-name").getDocuments { snapshot, error in
        if let error = error {
            print("Error fetching documents: \(error)")
            return
        }
        
        guard let documents = snapshot?.documents else {
            print("No documents found")
            return
        }
        
        // Clear existing data before adding new entries
        self.sources.removeAll()
        
        for doc in documents {
            if let source = Sourse(dictionary: doc.data()) {
                self.sources.append(source)
            } else {
                print("Failed to parse document: \(doc.data())")
            }
        }
        
        // Reload TableView on main thread (UI updates must happen here)
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

Don’t forget to call fetchData() somewhere like viewDidLoad() to kick off the data fetch.

Then, double-check your TableView data source methods are using the sources array correctly:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sources.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath)
    let source = sources[indexPath.row]
    cell.textLabel?.text = source.name
    cell.detailTextLabel?.text = source.content
    // Format the timestamp for display if needed
    return cell
}

Finally, confirm that your Firestore documents have the exact field names: name (string), content (string), and timestamp (stored as a Date type, not a string). If you saved the timestamp as a string, you’ll need to adjust the initializer to parse it into a Date object.

Give these fixes a try—chances are the typo and missing main thread reload were the main culprits!

内容的提问来源于stack exchange,提问作者Matt Manion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:32:04