首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >与UIsearchController有关的问题

与UIsearchController有关的问题
EN

Stack Overflow用户
提问于 2018-08-30 05:18:05
回答 1查看 0关注 0票数 0

嗨我有UISearchController问题,当我搜索数据并且过滤后的数据显示正常时,但是当我删除搜索中的文本时,所有数据(默认数据)都没有显示到表视图中并仍然保存第一个索引在tableview中我已经搜索过它的那个,我附上了视频,这里是代码:

代码语言:javascript
复制
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()
    }

}

这是表数据源

代码语言:javascript
复制
 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的是长代码

代码语言:javascript
复制
 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

    }
}

这里上传的视频显示了我的问题

http://www.up-00.com/downloadf-153556592762831-mp4.html

EN

回答 1

Stack Overflow用户

发布于 2018-08-30 14:46:54

我建议你创建第二个数组 [gameStruct]

代码语言:javascript
复制
var allGames = [gameStruct]()
var filteredArray = [gameStruct]()

然后在viewDidLoad()你拿到filteredArray这样的东西的时候。让我们allGames更好地理解主阵列。

代码语言:javascript
复制
//your fetching func 
func fetchGames() {
  // your code
  self.allGames.append(game) // I think in your code it looks like that :)
  self.filteredArray = self.allGames
}

下一个

代码语言:javascript
复制
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        filteredArray = allGames
        self.tableView.reloadData()
    } 
}

如果它不起作用,我会在明天尝试一下。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档