嗨我有UISearchController问题,当我搜索数据并且过滤后的数据显示正常时,但是当我删除搜索中的文本时,所有数据(默认数据)都没有显示到表视图中并仍然保存第一个索引在tableview中我已经搜索过它的那个,我附上了视频,这里是代码:
var searchBar :UISearchController!
var shouldShowSearchResults = false
var filteredArray = [gameStruct]() self.searchBar = UISearchController(searchResultsController: nil)
if #available(iOS 11.0, *) {
navigationItem.searchController = searchBar
} else {
// Fallback on earlier versions
self.tableView.tableHeaderView = self.searchBar.searchBar
}
self.searchBar.searchResultsUpdater = self
self.searchBar.dimsBackgroundDuringPresentation = true
self.searchBar.searchBar.sizeToFit()
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
shouldShowSearchResults = true
}
func didDismissSearchController(_ searchController: UISearchController) {
shouldShowSearchResults = false
retrieveData()
reloadData()
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
shouldShowSearchResults = false
retrieveData()
reloadData()
tableView.reloadData()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
shouldShowSearchResults = false
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
shouldShowSearchResults = false
}
func searchBarIsEmpty() -> Bool {
// Returns true if the text is empty or nil
return searchBar.searchBar.text?.isEmpty ?? true
}
func isFiltering() -> Bool {
return searchBar.isActive && !searchBarIsEmpty()
}
func updateSearchResults(for searchController: UISearchController) {
if searchBar.searchBar.text == nil || searchBar.searchBar.text == "" {
shouldShowSearchResults = false
gamesArray = gameV3 as [AnyObject]
view.endEditing(true)
}else{
shouldShowSearchResults = true
filteredArray = gameV3.filter() {
let strGameName = $0.gameName
let stringToCompare = searchBar.searchBar.text!
if strGameName.range(of: stringToCompare) != nil {
return true
} else {
return false
}
}
tableView.reloadData()
}
}
这是表数据源
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier! == "pushDetailView") {
if #available(iOS 10.0, *) {
if let destination = segue.destination as? DetailViewController {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.prepare()
generator.impactOccurred()
var indexPath = self.tableView.indexPathForSelectedRow!
//get the object for the selected row
if filteredArray.count > 0 {
let gameV2 = filteredArray[indexPath.row]
let g = games (gameName: gameV2.gameName, andGameID: gameV2.id, andGameDate: gameV2.gameDate, andGameImage: gameV2.image, andGameSite: gameV2.gameSite, andGameVideo: gameV2.gameVideo)
destination.getGame(g)
} else {
let object = gamesArray[indexPath.row]
destination.getGame(object)
}
}
} else {
// Fallback on earlier versions
}
}
}
而这个用于indexPath的是长代码
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellID = "Cell"
let cell:customCell = self.tableView.dequeueReusableCell(withIdentifier:CellID) as! customCell
// Configure the cell...
cell.backgroundColor = UIColor.clear
if shouldShowSearchResults{
var gamesObject: gameStruct!
gamesObject = filteredArray[indexPath.row]
cell.gameName.text = gamesObject.gameName
cell.gameName.textColor = UIColor.white
let url = NSURL(string: "http://www.ya-techno.com/gamesImage/\(gamesObject.image)")
do{
cell.gameThum2.layer.cornerRadius = 10.0
cell.gameThum2.layer.masksToBounds = true
cell.gameThum2.layer.borderWidth = 0.8
cell.gameThum2.layer.borderColor = UIColor.red.cgColor
cell.gameThum.sd_setImage(with: url as! URL)
cell.gameThum2.sd_setImage(with: url as! URL)
}catch{
print("error")
}
cell.gameName.textColor = UIColor.white
cell.backgroundColor = .black
} else {
var gamesObject: games!
gamesObject = gamesArray[indexPath.row] as! games
cell.gameName.text = gamesObject.gameName
cell.gameName.textColor = UIColor.white
let url = NSURL(string: "http://www.ya-techno.com/gamesImage/\(gamesObject.gameImage)")
do{
cell.gameThum2.layer.cornerRadius = 10.0
cell.gameThum2.layer.masksToBounds = true
cell.gameThum2.layer.borderWidth = 0.8
cell.gameThum2.layer.borderColor = UIColor.red.cgColor
cell.gameThum.sd_setImage(with: url as! URL)
cell.gameThum2.sd_setImage(with: url as! URL)
}catch{
print("error")
}
cell.gameName.textColor = UIColor.white
cell.backgroundColor = .black
}
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowSearchResults{
return filteredArray.count
} else {
return gamesArray.count
}
}
这里上传的视频显示了我的问题
发布于 2018-08-30 14:46:54
我建议你创建第二个数组 [gameStruct]
var allGames = [gameStruct]()
var filteredArray = [gameStruct]()
然后在viewDidLoad()
你拿到filteredArray
这样的东西的时候。让我们allGames
更好地理解主阵列。
//your fetching func
func fetchGames() {
// your code
self.allGames.append(game) // I think in your code it looks like that :)
self.filteredArray = self.allGames
}
下一个
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
filteredArray = allGames
self.tableView.reloadData()
}
}
如果它不起作用,我会在明天尝试一下。
https://stackoverflow.com/questions/-100002494
复制相似问题