Swift Parse - local datastore items not appearing in tableView

我是研究僧i 提交于 2019-12-12 03:18:48

问题


This is a followup of my question from yesterday yesterdays post

I have successfully saved and object in the local datastore using parse, and and trying to add that object to an array to store them so i can display the contents in a table view. the query is running fine, but it appears to me that nothing is being appended into the array, so nothing shows in the table view. here's my code.

localData.swift file

import Foundation

struct localData {
var date: String!
var latt: NSNumber!
var lattDelta: NSNumber!
var locality: String!
var longi: NSNumber!
var longiDelta: NSNumber!
var name: String!
var note: String!
}

I then declare this globally:

var arrayToPopulateCells = [localData]()

this is my parse query:

func performQuery() {
    let query = PFQuery(className: "ParseLighthouse")

    query.fromLocalDatastore()
    query.whereKey("User", equalTo: PFUser.currentUser()!)
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) lighthouses.")
            // Do something with the found objects
            if let light = objects as? [PFObject] {
                for object in light {
//                        println(object.objectId)
//                        println(object.objectForKey("Name"))
//                        println(object.objectForKey("Locality"))
                    var singleData = localData()
                    singleData.name = object["Name"] as! String
                    singleData.note = object["Note"] as! String
                    singleData.date = object["Date"] as! String
                    singleData.latt = object["Latt"] as! NSNumber
                    singleData.longi = object["Longi"] as! NSNumber
                    singleData.lattDelta = object["LattDelta"] as! NSNumber
                    singleData.longiDelta = object["LongiDelta"] as! NSNumber
                    singleData.locality = object["Locality"] as! String


                    self.arrayToPopulateCells.append(singleData)



                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
}

in my table code:

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

and

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



//        var lighthouse = self.lighthouses[indexPath.row]
    var data = self.arrayToPopulateCells[indexPath.row]

    //setting the prototype cell to link with the identifier set in attributes earlier.
    let cell = tableView.dequeueReusableCellWithIdentifier("locationCell") as! lighthouseCell

    let row = indexPath.row
    cell.cellName.text = data.name
    cell.cellPlace.text = data.locality
//        cell.cellCoordinates.text = "\(lighthouse.latt)" + ", " + "\(lighthouse.longi)"
//        cell.cellNote.text = lighthouse.note
    cell.cellDate.text = "\(data.date)"



    return cell
}

so im not sure what i'm doing wrong, but it seems that the query is working but nothing is going into the array. any ideas?

i do want to note that the parse object is created on lets say viewcontroller #2, and the query is run on viewcontroller #1 where the table view is. does this make a difference? should i run the query and try to append right after the object is made on the same controller?


回答1:


I think your problem is you need to call

self.tableView.reloadData()

outside the for object in light { loop

I think your data is being added to the array ok, its just the table view needs to know when its done.

EDIT***

func performQuery() {
let query = PFQuery(className: "ParseLighthouse")

query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
    if error == nil {
        // The find succeeded.
        println("Successfully retrieved \(objects!.count) lighthouses.")
        // Do something with the found objects
        if let light = objects as? [PFObject] {
            for object in light {
                var singleData = localData()
                singleData.name = object["Name"] as! String
                singleData.note = object["Note"] as! String
                singleData.date = object["Date"] as! String
                singleData.latt = object["Latt"] as! NSNumber
                singleData.longi = object["Longi"] as! NSNumber
                singleData.lattDelta = object["LattDelta"] as! NSNumber
                singleData.longiDelta = object["LongiDelta"] as! NSNumber
                singleData.locality = object["Locality"] as! String
                self.arrayToPopulateCells.append(singleData)
            }
            self.tableView.reloadData()
        }
    } else {
        // Log details of the failure
        println("Error: \(error!) \(error!.userInfo!)")
    }
}
}


来源:https://stackoverflow.com/questions/32442751/swift-parse-local-datastore-items-not-appearing-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!