在UITableView中从query.whereKey("列",nearGeoPoint)反向排序

时间:2015-03-01 04:40:27

标签: uitableview swift parse-platform geopoints

我试图从parse.com获取位置数据(PFGeoPoint),在UITableView中显示它,并从用户位置按最近的一个排序。

我已经使用了与解析文档中显示的相同的代码:

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

我设法得到了数据。我还设法在我的UITableView中显示它。问题是,订单是相反的。我的第一个牢房里的距离最远。谁能解释为什么会发生这种情况,以及如何解决这个问题?

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
    }
}

1 个答案:

答案 0 :(得分:1)

您是否故意使用reverseObjectEnumerator?因为这可能会导致您的结果被反转...线索在方法名称中;-)

如果从代码中删除以下两行,则可能不再反转。

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