Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

ios - Swift Parse - local datastore items not appearing in tableView

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!)")
    }
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...