使用 searchController 过滤 tableView,但 tableView 没有更新

Using searchController to filter tableView, but the tableView isn't updating

我有一个 UITableViewController 显示我创建的标签的标题。当我第一次导航到 UITableViewController 时,它显示标签的 Array 就好了,但是当我使用 UISearchController 过滤标签时,我创建的 Array 存储过滤后的结果会更新并保留正确的数据,但 TableView 不会改变。这是最有可能导致问题的两个函数,但为了以防万一,我将整个 class(不长)放在下面。
numberOfRowsInSection:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchController.searchBar.text != "") {
        return filteredTags.count
    }
    return Tags.count
}

cellForRowAt:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tagcell", for: indexPath) as! TagCell

    var text = ""
    if (searchController.searchBar.text != ""){
        text = filteredTags[indexPath.row].title
    } else {
        text = Tags[indexPath.row].title
    }

    cell.cellLabel.text = text
    return cell
}

整个Class:

class TagCell: UITableViewCell{

    @IBOutlet weak var cellLabel: UILabel!
}

class TagTableVC: UITableViewController{

    //Table Content

    var Tags: [Tag] = [globTS.animals, globTS.civilrights, globTS.guncontrol, globTS.gunrights, globTS.LGBTQ, globTS.prochoice, globTS.prolife]
    var filteredTags = [Tag]()

    //Searchbar Initialization
    let searchController = UISearchController(searchResultsController: nil)

    //Required Functions
    override func viewDidLoad() {
        super.viewDidLoad()
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(searchController.searchBar.text != "") {
            return filteredTags.count
        }
        return Tags.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tagcell", for: indexPath) as! TagCell

        var text = ""
        if (searchController.searchBar.text != ""){
            text = filteredTags[indexPath.row].title
        } else {
            text = Tags[indexPath.row].title
        }

        cell.cellLabel.text = text
        return cell
    }

    //Filters Tags array into Filtered array based on search query
    func filterContentForSearchText(searchText: String, scope: String = "All"){
        filteredTags = Tags.filter{ [=12=].title.lowercased().contains(searchText.lowercased())}
    }
}

extension TagTableVC: UISearchResultsUpdating {

    //calls the filter function everytime the searchbar is activated
    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    }
}

重新评估过滤后的标签后,您应该在 tableview 上调用 reloadData

    func filterContentForSearchText(searchText: String, scope: String = "All"){
        filteredTags = Tags.filter{ [=10=].title.lowercased().contains(searchText.lowercased())}
        self.tableView.reloadData()
    }