Swift3 - 按位置排序tableview

时间:2016-11-22 11:24:04

标签: ios swift uitableview swift3

我尝试按距离从当前位置对tableView进行排序。我在网站上搜索,但是我很难适应我发现的项目

你能帮我吗?

这是我的第一个数据文件“Cables.swift”:

import Foundation
import MapKit

class Person {
    var identifier:Int
    var name:String
    var country:String
    var email:String
    var website:String
    var facebook:String
    var adress:String
    var phone:String
    var latitude:Double
    var longitude:Double
    var lac:Int
    var poulie1:Int
    var rotation1:String
    var module1:Int
    var poulie2:Int
    var rotation2:String
    var module2:Int

    var distance:Double

    var smallPhotoUrl:URL! {
        return URL(string: "http://wakefinder.16mb.com/full/\(self.identifier).jpg")
    }

    var largePhotoUrl:URL! {
        return URL(string: "http://wakefinder.16mb.com/full/\(self.identifier).jpg")
    }

    //var coordinate: CLLocationCoordinate2D

    init?(fromData personData:[String:AnyObject]) {
        guard let identifier = personData["id"] as? Int,
            let name = personData["name"] as? String,
            let country = personData["country"] as? String,
            let email = personData["email"] as? String,
            let website = personData["website"] as? String,
            let facebook = personData["facebook"] as? String,
            let adress = personData["adress"] as? String,
            let phone = personData["phone"] as? String,
            let latitude = personData["lat"] as? Double,
            let longitude = personData["lng"] as? Double,
            let lac = personData["lac"] as? Int,
            let poulie1 = personData["poulie1"] as? Int,
            let rotation1 = personData["rotation1"] as? String,
            let module1 = personData["module1"] as? Int,
            let poulie2 = personData["poulie2"] as? Int,
            let rotation2 = personData["rotation2"] as? String,
            let module2 = personData["module2"] as? Int
            else {
                return nil
        }

        self.identifier = identifier
        self.name = name
        self.country = country
        self.email = email
        self.website = website
        self.facebook = facebook
        self.adress = adress
        self.phone = phone
        self.latitude = latitude
        self.longitude = longitude
        self.lac = lac
        self.poulie1 = poulie1
        self.rotation1 = rotation1
        self.module1 = module1
        self.poulie2 = poulie2
        self.rotation2 = rotation2
        self.module2 = module2
    }
    // Function to calculate the distance from given location.
    func calculateDistance(fromLocation: CLLocation?) {
        let location = CLLocation(latitude: self.latitude, longitude: self.longitude)
        distance = location.distance(from: fromLocation!)
    }
}

和我的TableView文件:

import UIKit
import Alamofire
import CoreLocation

class cableViewController: UITableViewController, UISearchResultsUpdating {
    var _personList:[Person] = []
    var _personFiltered:[Person] = []
    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        Alamofire.request("http://wakefinder.16mb.com/users.json")
            .validate()
            .responseJSON { (response) in
                if response.result.isSuccess {
                    let rawPersonList = response.result.value as! [[String:AnyObject]]
                    for personData in rawPersonList {
                        if let personObject = Person(fromData: personData) {
                            self._personList.append(personObject)
                        }
                    }
                    self.tableView.reloadData()
                } else {
                    print(response.result.error as Any)
                }
        }
    }


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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchController.isActive && searchController.searchBar.text != "" {
            return _personFiltered.count
        }
       return _personList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:cablesTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cable_cell") as! cablesTableViewCell
        if searchController.isActive && searchController.searchBar.text != "" {
            let person:Person = _personFiltered[indexPath.row]
            cell.display(person: person)
            //_personList[indexPath.row] = _personFiltered[indexPath.row]
        } else {
            let person:Person = _personList[indexPath.row]
            cell.display(person: person)
        }
        return cell
        }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let cell = sender as? UITableViewCell {
                if let indexPath = self.tableView.indexPath(for: cell) {
                    let selectedPerson: Person
                    if searchController.isActive && searchController.searchBar.text != "" {
                        selectedPerson = _personFiltered[indexPath.row]
                    } else {
                        selectedPerson = _personList[indexPath.row]
                    }
                    let personViewController:fichesViewController = segue.destination as! fichesViewController
                    personViewController._person = selectedPerson
                }

            }
        }
    }

    func updateSearchResults(for searchController: UISearchController) {
        filterContent(searchText: self.searchController.searchBar.text!)
    }

    func filterContent(searchText:String) {
        _personFiltered = _personList.filter { user in
            let username = user.name
            return(username.lowercased().contains(searchText.lowercased()))
        }
        self.tableView.reloadData()
    }
}

0 个答案:

没有答案