当我删除它们时,UISearchController 不会删除结果

UISearchController does not delete the result when I delete them

我在我的应用程序中实现了 UISearchController 来搜索要关注和取消关注的用户。我正在使用 swift 和 Parse 作为后端。问题是,每当我想输入一些东西然后删除它时,它会保留检索到的结果并且不会删除它们。当没有结果时,table 视图显示 "No result" 检索结果,因为没有删除!! 我不知道是什么问题。有什么帮助吗?

这是代码,

UISearchController 委托和方法,

// To search
 var userSearchController: UISearchController!
 var resultsController = UITableViewController()
 var searchActive: Bool = false

// To follow and unfollow users
var userIds = [""]
var isFollowing = ["":false]


// MARK: - Lifecycle

override func viewDidLoad() {
    super.viewDidLoad()

    // tableview delegate and datasource
    self.resultsController.tableView.dataSource = self
    self.resultsController.tableView.delegate = self

    self.resultsController.loadViewIfNeeded()
    configureSearchController()
}

func configureSearchController(){

    self.userSearchController = UISearchController(searchResultsController: nil)
    // Set the search controller to the header of the table
    self.tableView.tableHeaderView = self.userSearchController.searchBar
    // This is used for dynamic search results updating while the user types
    // Requires UISearchResultsUpdating delegate
    self.userSearchController.searchResultsUpdater = self
    self.userSearchController.dimsBackgroundDuringPresentation = false
    self.definesPresentationContext = true

    // Configure the search controller's search bar
    self.userSearchController.searchBar.placeholder = "Search for a parent"
    self.userSearchController.searchBar.sizeToFit()
    self.userSearchController.searchBar.delegate = self
}

// MARK: - Search Bar Delegate Methods

func searchBarSearchButtonClicked(searchBar: UISearchBar) {

    let searchString: String = searchBar.text!.lowercaseString
    if searchActive == false {
        searchActive = true
        // Force search if user pushes button
        if (searchString != "") {
            loadSearchUsers(searchString)
        }

        self.resultsController.tableView.reloadData()

    }
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    searchActive = true

}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchActive = false

}


func searchBar(searchBar: UISearchBar, textDidChange searchText: String){

    if (searchBar.text == ""){
        searchActive = false
    }
    else {
        searchActive = true
        let searchString: String = searchBar.text!.lowercaseString
        if (searchString != ""){
            loadSearchUsers(searchString)}
    }

}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    searchActive = false
    searchBar.text = ""
    self.searchUsers.removeAll(keepCapacity: false)

    self.resultsController.tableView.reloadData()

}

func updateSearchResultsForSearchController(searchController: UISearchController) {

    if let searchString: String = searchController.searchBar.text!.lowercaseString {

        if (searchString != "" && !self.searchActive) {
            loadSearchUsers(searchString)
        }
    }
}

这是 numberOfSectionsInTableView

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    var numOfSection: NSInteger = 0

     if userIds.count > 0 {
        //self.resultsController.tableView.reloadData()
        self.tableView.backgroundView = nil
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
        numOfSection = 1


    } else {

        let noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
        noDataLabel.text = "No result."
        noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 180.0/255.0, alpha: 1.0)
        noDataLabel.textAlignment = NSTextAlignment.Center
        self.tableView.backgroundView = noDataLabel
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    }

    return numOfSection
}

如果您需要更多信息,请告诉我。非常感谢。

Table 数据不会因为您更改了搜索文本而被删除。您必须先清除数据集,然后将搜索结果设置到数据集中,然后重新加载,您已经在 searchBarCancelButtonClicked 方法中执行了此操作。

self.searchUsers.removeAll()
self.searchUsers.appendContentsOf(results)
self.resultsController.tableView.reloadData()

也许你忘记了 table.reloadData()。试试这个代码

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var table: UITableView!

var dataArray = [1 : "first" ,2 : "second",3 :"third",]
var filteredArray = [String]()
var gotResult = false

override func touchesBegan(touches: Set<UITouch>, withEvent event:   UIEvent?)
{
    self.view.endEditing(true);
    self.searchBar.endEditing(true)
    gotResult = false
    table.reloadData()
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar)
{
    gotResult = true
    table.reloadData()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar)
{
    if !gotResult
    {
        gotResult = true
        table.reloadData()
    }
    searchBar.resignFirstResponder()
}

func searchBar(searchBar: UISearchBar, textDidChange searchedText: String)
{

    filteredArray = dataArray.values.filter({ (Items) -> Bool in
        let currentItem: NSString = Items

        return (currentItem.rangeOfString(searchedText, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
    })

    self.table.reloadData()
}
//Codes for table 
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if gotResult {
        return filteredArray.count
    }
    else {
        return dataArray.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell();
    let value = Array(dataArray.values)[indexPath.row]

    if gotResult {
        cell.textLabel?.text = filteredArray[indexPath.row]
    }
    else {
        cell.textLabel?.text = value

    }

    return cell
}