0

I'm trying to get location data(PFGeoPoint) from parse.com, show it in UITableView, and sort it by nearest one from user location.

I already use the code same with shown in parse documentation :

findPlaceData.whereKey("position", nearGeoPoint:SearchLocationGeoPoint)

I managed to get the data. I also managed to show it in my UITableView. The problem is, the order is reversed. I got the farthest in my first cell. Could anyone explain why this happen, and how to fix it?

import UIKit

class SearchTableViewController: UITableViewController {

    @IBOutlet var SearchTitle: UILabel!
    var userLocationToPass:CLLocation!
    var categoryToPass:String!
    var categoryIdToPass:String!
    var placeData:NSMutableArray! = NSMutableArray()

    override init(style: UITableViewStyle) {
        super.init(style: style)
        // Custom initialization
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }


    func loadData(){
        placeData.removeAllObjects()
        let searchLocationGeoPoint = PFGeoPoint(location: userLocationToPass)
        var findPlaceData:PFQuery = PFQuery(className: "Places")
        findPlaceData.whereKey("category", equalTo: categoryIdToPass)
        findPlaceData.whereKey("position", nearGeoPoint:searchLocationGeoPoint)
        findPlaceData.findObjectsInBackgroundWithBlock{
            (objects:[AnyObject]!, error:NSError!)->Void in

            if error == nil{
                for object in objects{
                    let place:PFObject = object as PFObject
                    self.placeData.addObject(place)
                }

                let array:NSArray = self.placeData.reverseObjectEnumerator().allObjects
                self.placeData = NSMutableArray(array: array)

                self.tableView.reloadData()

            }

        }
    }

    override func viewWillAppear(animated: Bool) {
        loadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        SearchTitle.text = categoryToPass
        println(userLocationToPass)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        return 1
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:SearchTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SearchTableViewCell

        let place:PFObject = self.placeData.objectAtIndex(indexPath.row) as PFObject
        cell.placeName.text = place.objectForKey("name") as? String
        cell.placeOpenHour.text = place.objectForKey("openhour") as? String

        return cell
    }
}
Gibran
  • 934
  • 2
  • 8
  • 19

1 Answers1

1

Are you intentionally using the reverseObjectEnumerator? Because that could account for your results being reversed... The clue is in the method name ;-)

If you drop the following two lines from your code, it might not be reversed anymore.

let array:NSArray = self.placeData.reverseObjectEnumerator().allObjects
self.placeData = NSMutableArray(array: array) 
rickerbh
  • 9,731
  • 1
  • 31
  • 35