使用 UISearchBar 在 UITableView 上搜索 Swift 5

Searching on UITableView Using UISearchBar Swift 5

** 注意:两个不同的数组 TopicTitle 和 TopicDesc**

I want to filter data by TopicTitle and it is working fine

 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    filteredTitleArray = searchText.isEmpty ? titleArray : titleArray.filter { title -> Bool in
        return title.contains(searchText)
    }
    tableView.reloadData()
}

Problem is, i am getting TopicTitle filtered but the TopicDescription is not showing according to filtered Cell

不要使用多个数组,使用一个结构

struct Topic {
    let title, desc : String
}

并声明(数据源)数组

var topicArray = [Topic]()
var filteredArray = [Topic]()

然后过滤

filteredArray = searchText.isEmpty ? topicArray : topicArray.filter { topic -> Bool in
    return topic.title.range(of: searchText, options: .caseInsensitive) != nil
}

range(of 语法的好处是搜索不区分大小写的能力。