ios - 我正在尝试按位置对用户的帖子进行排序并使用 PFGeopoint 和 Swift 显示距离

标签 ios swift uitableview parse-platform

我正在尝试按距离对用户的帖子进行排序,并隐藏任何超过特定英里数的帖子。我还尝试在我的 UITableview 中从最近到最远进行排序:

class FindAParty:UITableViewController{

    //var partyData:NSMutableArray! = NSMutableArray()
    var partyData = [PFObject]()
    //var user:NSMutableArray = NSMutableArray()

    override init(style: UITableViewStyle){
        super.init(style: style)

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //fatalError("init(coder:) has not been implemented")

    }


    @IBAction func loadData(){

        PFGeoPoint.geoPointForCurrentLocation { (geopoint, error) in

            print(geopoint)
            if let geopoint = geopoint {
                PFUser.current()?.remove(forKey: "Location")

                print ("True")
            PFUser.current()? ["Location"] = geopoint
                PFUser.current()?.saveInBackground()


             }

        }




        print ("Load Data went through")
        partyData.removeAll()
        print ("Remove ALL Objeccts")
        let findPartyData = PFQuery(className: "Party")

        print("PFQuery...")
        findPartyData.findObjectsInBackground {
            (objects:[PFObject]?, error:Error?)->Void in

            if error != nil {
                print(error!)
            } else{
                if let objects = objects {
                    self.partyData = objects.reversed()
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
    override func viewDidAppear(_ animated: Bool) {
        self.loadData()
        print("View Did Appear")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        print("ViewDidLoad")
        //self.loadData()
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // #pragma mark - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return partyData.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FindAPartyCell

       let party = self.partyData[indexPath.row]





        cell.typeOfPartyLabel.alpha = 0
        cell.timeOfPartyLabel.alpha = 0
        cell.usernameLabel.alpha = 0
        cell.alcoholTFLabel.alpha = 0

        cell.typeOfPartyLabel.text = party.object(forKey: "partyTitle") as? String
        cell.timeOfPartyLabel.text = party.object(forKey: "partyTime") as? String
        cell.usernameLabel.text = party.object(forKey: "Usernames") as? String
        cell.alcoholTFLabel.text = party.object(forKey: "ATF") as? String

       // var dataFormatter:NSDateFormatter = NSDateFormatter()
        //dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
        //cell.timestampLabel.text = dataFormatter.stringFromDate(sweet.createdAt)

       /* let findUser:PFQuery = PFUser.query()!
        findUser.whereKey("objectId", equalTo: party.object(forKey: "Username")!)


        findUser.findObjectsInBackground {
            (objects:[PFObject]?, error: Error?) -> Void in // Changes NSError to Error
            if error == nil{

                let user:PFUser = (objects)!.last as! PFUser
                cell.usernameLabel.text = user.username

               */ UIView.animate(withDuration: 0.5, animations: {
                    cell.typeOfPartyLabel.alpha = 1
                    cell.timeOfPartyLabel.alpha = 1
                    cell.usernameLabel.alpha = 1
                    cell.alcoholTFLabel.alpha = 1
                })
            //}
        //}


        return cell
    }


}

如果可能的话,我也想显示距离。 感谢您的任何建议

最佳答案

您可以使用 Parse 提供的地理查询。

whereKey:nearGeoPoint 返回按距离排序的对象数组(从最近到最远)

whereKey:nearGeoPoint:withinMiles/whereKey:nearGeoPoint:withinKilometers 使用距离(m/km)限制结果

  1. 按“位置”查询用户(您应该考虑对属性使用小写字母) 让查询 = PFUser.query()! query.whereKey("位置", nearGeoPoint: geopoint)
  2. 获取他们的帖子...(单独的帖子表?使用关系?)

关于ios - 我正在尝试按位置对用户的帖子进行排序并使用 PFGeopoint 和 Swift 显示距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42215594/

相关文章:

ios - 在应用程序商店提交应用程序时选择了 xc adhoc * dist 配置文件的应用程序是否得到了苹果的批准?

ios - Swift 1.2 - 为什么我的 UIPickerView 不显示我的数据?

ios - Objective-c 中的 NSURLSessionUploadTask 示例?

ios - 代码 8 : UI tests can't find library being tested

ios - ViewController 关闭 swift ios

iOS 图表库和 LineChart xAxis

ios - swift 从 ALAsset 获取图像

iphone - 在 UITableViewCell 上实现我自己的滑动

ios - 为什么我的 TableView 不起作用?

iphone - 使用图像作为 UITableView 分隔符