我正在尝试从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
}
}
最佳答案
您是否有意使用reverseObjectEnumerator
?因为那可能导致您的结果被逆转...线索在方法名称中;-)
如果从代码中删除以下两行,则可能无法再将其反转。
let array:NSArray = self.placeData.reverseObjectEnumerator().allObjects
self.placeData = NSMutableArray(array: array)