如何从解析中检索geoPoints并将其存储在tableView中?

时间:2016-04-04 13:37:08

标签: swift uitableview parse-platform

您好我有一个应用程序,它将名称存储在一个解析服务器中,以便我能够将数据检索到控制台,但我无法用它们填充tableView。我怎么能这样做?谢谢!

这是我用来存储数据的代码:

let geoPoint = PFObject(className: "location")

geoPoint["location"] = activePlace
geoPoint["spot"] = places
geoPoint.saveInBackgroundWithBlock { (succes, error) -> Void in
    print("place has been saved")
}

这是我用来检索它的代码:

let query = PFQuery(className: "location")
query.findObjectsInBackgroundWithBlock { (object, error) in


    if error != nil {

        print(error)

    }else {

        print(object)
        self.tableView.reloadData()

    }
}

这就是我确定tableView的方法:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return places.count


}

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = places[indexPath.row]["name"]


    return cell



}

这就是确定“地点”的方式:

var places = [Dictionary<String,String>()]

以下是我在解析服务器上的内容:

![This in from the parse server] 1

感谢您的帮助!

编辑:这是我在控制台中得到的:

Optional([<location: 0x7fd383e34940, objectId: K4E1Dn0qqV, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f2e070>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f30600, objectId: D4tdDJcA4x, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f14370>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f328c0, objectId: aEyiGvNmze, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f32c00>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f32f80, objectId: r9DVe2KJde, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f33320>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f336a0, objectId: 9LwM4ATHFC, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f14140>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        },
                {
            lat = "37.3246293922446";
            lon = "-122.023606373578";
            name = other;
        }
    );
}, <location: 0x7fd383f33c00, objectId: QM8rg2NKNf, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f34040>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        },
                {
            lat = "37.3246293922446";
            lon = "-122.023606373578";
            name = other;
        }
    );
}, <location: 0x7fd383f343c0, objectId: NjlsykJtTf, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f34690>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383f34a10, objectId: qdOPtRVzew, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f34c70>";
    location = "-1";
    spot =     (
    );
}, <location: 0x7fd383f34ff0, objectId: DC5ZW7WRNO, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f35250>";
    location = "-1";
    spot =     (
    );
}, <location: 0x7fd383f355d0, objectId: rCZIaHlceC, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f358a0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383f35c20, objectId: 2LI1u9KyNt, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f35ef0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383f36270, objectId: YKd0q9uQcY, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f365b0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3263892401666";
            lon = "-122.019776452882";
            name = "new one";
        }
    );
}, <location: 0x7fd383f36930, objectId: rGd3ChyOcV, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f36d60>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3263892401666";
            lon = "-122.019776452882";
            name = "new one";
        },
                {
            lat = "37.3247014690037";
            lon = "-122.020914298787";
            name = "mc laren";
        }
    );
}, <location: 0x7fd383f37110, objectId: aunhPqs1Ww, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f375a0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3263892401666";
            lon = "-122.019776452882";
            name = "new one";
        },
                {
            lat = "37.3247014690037";
            lon = "-122.020914298787";
            name = "mc laren";
        },
                {
            lat = "37.3246335613548";
            lon = "-122.023146659295";
            name = boom;
        }
    );
}, <location: 0x7fd383f37950, objectId: CtWCmwuZHv, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f37c60>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383cc37a0, objectId: AHHsEyrCJk, localId: (null)> {
    ACL = "<PFACL: 0x7fd383e228d0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3306871385499";
            lon = "-122.03026490349";
            name = "20405\U201320449 Mariani Ave";
        }
    );
}])

编辑2:这是整个tableView代码:

import UIKit
import Parse

var rideSpots = [""]

//test start

class Place {
    var ACL : String
    var location : String

    init(ACL : String, location : String) {
        self.ACL = ACL
        self.location = location
    }
}
let places2 : [Place] = []
//test stop

var places = [Dictionary<String,String>()]

var activePlace = -1

class TableViewController: UITableViewController {

    @available(iOS 8.0, *)
    func companyNameUpdatedAlert(title: String, error: String, indexPath: Int) {

        let alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)

        alert.addTextFieldWithConfigurationHandler { (textField) -> Void in

            textField.placeholder = "Enter new text"

        }

        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in

            let lat = places[indexPath]["lat"]!

            let lon = places[indexPath]["lon"]!

            places.removeAtIndex(indexPath)

            places.insert(["name" : alert.textFields![0].text!, "lat" : lat, "lon" : lon], atIndex: indexPath)

            self.tableView.reloadData()

            NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
            NSUserDefaults.standardUserDefaults().synchronize()


        }))

        self.presentViewController(alert, animated: true, completion: nil)


    }

    @available(iOS 8.0, *)
    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

        let changeText = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Change spot name" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in

            self.companyNameUpdatedAlert("Update text", error: "enter text below", indexPath: indexPath.row)

        })
     /*
        let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in

            places.removeAtIndex(indexPath.row)

            tableView.reloadData()

            NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
            NSUserDefaults.standardUserDefaults().synchronize()

        })
        */
        return [changeText]


    }

    override func viewDidLoad() {



            super.viewDidLoad()

        //retreve the data from parse start

        //first start of test retreving

        let query = PFQuery(className: "location")
        query.findObjectsInBackgroundWithBlock { (objects, error) in


            if error != nil {

                print(error)

            }else {



                print(object)

                for object in (objects)! {

                    // Retrieve data needed and add to places (example ACL and location)
                    let ACL : String = object["ACL"] as! String
                    let location : String = object["location"] as! String
                    let place : Place = Place(ACL : ACL, location : location)
                    self.places.append(place)
                }

                self.tableView.reloadData()

            }
        }


        //end of test retreving

    /*
        let query = PFQuery(className: "location")
        query.findObjectsInBackgroundWithBlock { (object, error) in


            if error != nil {

                print(error)

            }else {

                print(object)
                self.tableView.reloadData()

            }
        }
    */

            //retreve the data from parse stop


            if places.count == 1 {

                places.removeAtIndex(0)

                places.append(["name":"GO to map to add spot","lat":"90","lon":"90"])




            }
            if NSUserDefaults.standardUserDefaults().objectForKey("places") != nil {

                places = NSUserDefaults.standardUserDefaults().objectForKey("places") as! [Dictionary]


            }

        }


    override func didReceiveMemoryWarning() {

        didReceiveMemoryWarning()

    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return places.count


    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = places[indexPath.row]["name"]


        return cell



    }

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

        activePlace = indexPath.row

        return indexPath

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "newPlace" {

            activePlace = -1


        }

    }

    override func viewWillAppear(animated: Bool) {

    tableView.reloadData()


    }

}

1 个答案:

答案 0 :(得分:0)

经过一些审核后,我会创建一个名为places而不是字典的对象。这样你就可以创建一个对象数组而不是字典,这很奇怪。

class Place {
    var ACL : String
    var location : String

    init(ACL : String, location : String) {
        self.ACL = ACL
        self.location = location
    }
}

添加

var places : [Place] = []

在查询内部而不是仅打印对象并重新加载tableView。每次重新加载时,您的数据源都没有更改。

let query = PFQuery(className: "location")
query.findObjectsInBackgroundWithBlock { (objects, error) in


    if error != nil {

        print(error)

    }else {

        print(object)

        for object in objects {
            // Retrieve data needed and add to places (example ACL and location)
            let ACL : String = object["ACL"] as String
            let location : String = object["location"] as String
            let place : Place = Place(ACL : ACL, location : location)
            self.places.append(place)
        }

        self.tableView.reloadData()

    }
}