2017-02-13 2 views
0

Я пытаюсь сортировать сообщение пользователя на расстоянии и скрывать все, что было бы за определенный набор миль. Я также пытаюсь разобраться от ближайшей к Farthest все в моем UITableView:Я стараюсь сортировать сообщение пользователя по местоположению и отображать расстояние, используя PFGeopoint и Swift

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 
    } 


} 

Я хотел бы также, чтобы показать расстояние, если это возможно. Спасибо за любые советы

ответ

0

Вы можете использовать гео-запросы, предоставляемые Parse.

whereKey:nearGeoPoint Возвращает массив объектов, упорядоченных по расстоянию (ближайший к Farthest)

whereKey:nearGeoPoint:withinMiles/whereKey:nearGeoPoint:withinKilometers Limit результатов с использованием расстояния (м/км)

  1. Запрос пользователя по "Location" (вы должны рассмотреть возможность использования строчных символов для атрибутов) let query = PFUser.query()! query.whereKey("Location", nearGeoPoint: geopoint)
  2. Получите свои сообщения ... (Отдельные столбики сообщений? Использует отношения?)
Смежные вопросы