gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:06:59 24 4
gpt4 key购买 nike

我已经有一段时间可以使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com