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

标签 ios arrays swift uisearchcontroller

我已经有一段时间可以使用 UISearchController 了,但是我有一个小问题。

我的搜索 Controller 显示数组中的火车站列表。 (它们有很多,所以我将在下面的示例代码中减少数组),这一切都很好,我很满意。

问题是我还需要将火车站 GPS 坐标与车站名称一起传递。我一直在努力研究最好的方法来做到这一点,但遇到了困难。

我决定创建另一个包含站点代码、站点、纬度和经度的数组。

这是原始数组:

let data = ["Abbey Wood", "Aber", "Abercynon", "Aberdare", "Aberdeen", "Aberdour", "Aberdovey", "Abererch"]

这是新数组:

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()
    }


}

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

我的做法是完全错误的,还是我在正确的轨道上,只是做了一些愚蠢的事情?

最佳答案

我试图重现您的问题并编写了这段代码。我只使用 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()
    }
}

关于ios - 使用 Swift 将更多数据传递到 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30152698/

相关文章:

ios - Swift 协议(protocol) Where Constraint with == vs :

Ruby - 如果乘以等于第三个数字,则查找数组中是否有任何两个数字

JavaScript 数组性能在 13k-16k 元素时下降

ios - 无法将自动布局右 anchor 固定到 UITableView?

ios - 如何检查 Alamofire 因网络连接不良而失败?

ios - 将可拖动的 UIView 限制为 SuperView 的边界

android - 在 "org.json.JSONException: Value{data}"中获取 JSONArray 结果

ios - 如何实现具有通用约束类型属性的 Swift 协议(protocol)?

Swift - 选择随机函数

ios - 实现协议(protocol)以将数据从推送的 View Controller 传回 View Controller