使用Swift将更多数据传递到UISearchController

时间:2015-05-10 14:41:50

标签: ios arrays swift uisearchcontroller

我现在有一段时间工作let data = ["Abbey Wood", "Aber", "Abercynon", "Aberdare", "Aberdeen", "Aberdour", "Aberdovey", "Abererch"] ,不过我有一点问题。

我的搜索控制器显示阵列中的火车站列表。 (其中有很多,所以我将在下面的示例代码中删除数组),这一切都很好,我很满意。

问题是我需要通过火车站的GPS坐标和站名。我一直在努力研究最好的方法来做到这一点,但却遇到了障碍。

我决定创建另一个具有Station Code,Station,Latitude和Longitude的Array。

这是原始数组:

let stations = [Station(code: "ABW", name: "Abbey Wood", latitude: 51.4907705898, longitude: 0.1203255703),
    Station(code: "ABE", name: "Aber", latitude: 51.5749606879, longitude: -3.2298389345),
    Station(code: "ACY", name: "Abercynon", latitude: 51.6447060012, longitude: -3.3270007535),
    Station(code: "ABA", name: "Aberdare", latitude: 51.715057473, longitude: -3.4430991465),
    Station(code: "ABD", name: "Aberdeen", latitude: 57.1430482477, longitude: -2.0974804963),
    Station(code: "AUR", name: "Aberdour", latitude: 56.0545804403, longitude: -3.3005564433),
    Station(code: "AVY", name: "Aberdovey", latitude: 52.543972227, longitude: -4.0570808351),
    Station(code: "ABH", name: "Abererch", latitude: 52.8986004612, longitude: -4.3741959548)]

这是新阵列:

class Station {

    var code = ""
    var name = ""
    var latitude = 0.0
    var longitude = 0.0

    init(code: String, name: String, latitude: Double, longitude: Double) {
        self.code = code
        self.name = name
        self.latitude = latitude
        self.longitude = longitude
    }
}

我还写了这个以使用新数组:

UISearchController

问题在于我无法在 var filteredData: [String]! var searchController: UISearchController! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self tableView.delegate = self filteredData = data // Initializing with searchResultsController set to nil means that // searchController will use this view controller to display the search results searchController = UISearchController(searchResultsController: nil) searchController.searchResultsUpdater = self // If we are using this same view controller to present the results // dimming it out wouldn't make sense. Should set probably only set // this to yes if using another controller to display the search results. searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.searchBarStyle = .Minimal searchController.searchBar.sizeToFit() searchController.hidesNavigationBarDuringPresentation = false searchController.searchBar.showsCancelButton = false self.navigationItem.titleView = searchController.searchBar // Sets this view controller as presenting view controller for the search interface definesPresentationContext = true } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("TableCell") as! UITableViewCell cell.textLabel?.text = filteredData[indexPath.row] return cell } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return filteredData.count } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { // let cell = tableView.cellForRowAtIndexPath(indexPath)! selectedText = filteredData![indexPath.row] self.performSegueWithIdentifier("unwindToSet", sender: self) println("you pressed \(selectedText)") println(indexPath.row) } func updateSearchResultsForSearchController(searchController: UISearchController) { let searchText = searchController.searchBar.text filteredData = searchText.isEmpty ? data : data.filter({(dataString: String) -> Bool in return dataString.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil }) tableView.reloadData() } } 中实现此功能。

这是原始代码

{{1}}

无论我现在尝试做什么,我都无法将电台名称传递到tableview,

我是否认为这一切都错了,或者我是在正确的轨道而只是做一些愚蠢的事情?

1 个答案:

答案 0 :(得分:1)

我试图重现你的问题并写下了这段代码。我仅使用UITableViewUISearchBar来保持代码清洁。我使用Station个对象(searchBar(_:textDidChange:)更新了用于过滤数组的函数,您应该在updateSearchResultsForSearchController函数中使用这部分代码。

现在一切正常。

class Station {

    var code = ""
    var name = ""
    var latitude = 0.0
    var longitude = 0.0

    init(code: String, name: String, latitude: Double, longitude: Double) {
        self.code = code
        self.name = name
        self.latitude = latitude
        self.longitude = longitude
    }
}

class ViewController: UIViewController, UISearchBarDelegate {

    @IBOutlet weak var tableView: UITableView!
    var filteredData = [Station]()

    let stations = [Station(code: "ABW", name: "Abbey Wood", latitude: 51.4907705898, longitude: 0.1203255703),
        Station(code: "ABE", name: "Aber", latitude: 51.5749606879, longitude: -3.2298389345),
        Station(code: "ACY", name: "Abercynon", latitude: 51.6447060012, longitude: -3.3270007535),
        Station(code: "ABA", name: "Aberdare", latitude: 51.715057473, longitude: -3.4430991465),
        Station(code: "ABD", name: "Aberdeen", latitude: 57.1430482477, longitude: -2.0974804963),
        Station(code: "AUR", name: "Aberdour", latitude: 56.0545804403, longitude: -3.3005564433),
        Station(code: "AVY", name: "Aberdovey", latitude: 52.543972227, longitude: -4.0570808351),
        Station(code: "ABH", name: "Abererch", latitude: 52.8986004612, longitude: -4.3741959548)];


    override func viewDidLoad() {
        super.viewDidLoad()

        filteredData = stations
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("TableCell") as! UITableViewCell
        // Set name of station to textLabel
        cell.textLabel?.text = filteredData[indexPath.row].name 
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var selectedText = filteredData[indexPath.row].name
        println("you pressed \(selectedText)")
        println(indexPath.row)

    }

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        //Filter through Station objects
        filteredData = searchText.isEmpty ? stations : stations.filter({(station: Station) -> Bool in
            return station.name.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil
        })

        tableView.reloadData()
    }
}