从 UISearchController Swift 传递更多数据
Posted
技术标签:
【中文标题】从 UISearchController Swift 传递更多数据【英文标题】:Passing more data from UISearchController Swift 【发布时间】:2020-04-20 12:09:33 【问题描述】:我有一个 UISearchBar 和 tableview 显示数据。当我搜索时,结果正在获取并且能够传递搜索到的电影名称,但无法传递特定搜索结果的电影 ID,我尝试过但无法找到解决方案。以下是我尝试过的,请帮助,TIA。
// output of url
DataTable:
searchByCategory: [
movie_id: "56",
movie_name: "Avengers"
,
movie_id: "20",
movie_name: "Kong Skull Island"
,
.
.
.
]
// Swift file
var moviesArray = [[String:AnyObject]]()
var searchArr = [""]
var searching: Bool = false
var filtered:[String] = []
// Response
func getSearchResults(url: String, parameters: [String : String])
Alamofire.request(url, method: .post, parameters: parameters).responseJSON
response in
if response.result.isSuccess
print("Success! Got the response")
let responseData = JSON(response.result.value!)
if let resData = responseData["DataTable"]["searchByCategory"].arrayObject
self.moviesArray = resData as! [[String:AnyObject]]
for i in 0..<self.moviesArray.count
let dict = self.moviesArray[i]
let movieName = dict["movie_name"] as? String
self.searchArr.append(movieName!)
// numberOfRowsInSection
if searching
return filtered.count
else
return moviesArray.count
// cellForRowAt indexPath:
if searching
cell?.textLabel?.text = filtered[indexPath.row]
else
let dict = moviesArray[indexPath.row]
cell?.textLabel?.text = dict["movie_name"] as? String
// didSelectRowAt indexPath:
if(searching)
let movieDetails = MoviesViewController(nibName: "MoviesViewController", bundle: nil)
movieDetails.resDetails = self.filteredId[indexPath.row]
// Here am not able to pass the movie_id of the search results, i can only pass the movie name
navigationController?.pushViewController(movieDetails, animated: true)
else
// Search Method
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
filtered = searchArr.filter( (text) -> Bool in
let tmp: NSString = text as NSString
let range = tmp.range(of: searchText, options: .caseInsensitive)
return range.location != NSNotFound
)
if(filtered.count == 0)
searching = false;
else
searching = true;
self.searchList.reloadData()
【问题讨论】:
您想从结果中获取电影 ID 并附加到 searchArray 或其他内容吗?不清楚 我想将特定搜索的电影 id 传递给 didSelectRowAt indexPath 【参考方案1】: var isSearchNull: Bool = true
didSet
tableView.reloadData()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
var numberOfRows = 0
if isSearchNull
numberOfRows = moviesArray.count
else
numberOfRows = filtered.count
return numberOfRows
搜索委托
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
filtered.removeAll() // the array of search i guess its filtered
guard let textToSearch = searchBar.text, !textToSearch.isEmpty else
return
isSearchNull = textToSearch != "" ? false : true
if !isSearchNull
if Date().timeIntervalSince(previousRun) > minInterval
previousRun = Date()
fetchResults(getSearchResults(url : url ) // your networking process
和
func getSearchResults(url: String, parameters: [String : String])
//// network
fetchData() [weak self ]
guard let self = self else return
if let error = error
print(error)
guard let result = result, !result.isEmpty else return
self.filtered = result
最后
private var filtered:[String] = []
didSet
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1)
self.tableView.reloadData()
【讨论】:
以上是关于从 UISearchController Swift 传递更多数据的主要内容,如果未能解决你的问题,请参考以下文章
如何从 searchResultsController (UISearchController) 更新数据
从 UISearchController 搜索栏快速删除黑色边框
UITableView、UISearchController 和 MJRefresh,垂直滚动指示器从左到右闪烁
从 UISearchController Swift 传递更多数据