【发布时间】:2021-01-25 02:43:37
【问题描述】:
我的应用程序中的搜索栏无法正常工作。我已将问题缩小到方法 cellforRowAt。输入单词时无法显示搜索结果。 cellforRowAt 包含索引路径中过滤文章的信息。我该如何解决这个问题?
项目链接:https://github.com/lexypaul13/Covid-News.git
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //确定用户类型时应该使用什么数据源
如果是过滤{
返回filteredArticles?.count ?? 0
}
返回文章?。计数?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
让 cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as!新闻TableViewCell
让新闻:文章
过滤的文章 = 文章
如果是过滤{
新闻 = 过滤文章![indexPath.row]
}
别的{
新闻 = 文章![indexPath.row]
}
cell.authorName.text = 过滤文章?[indexPath.row].author
cell.headLine.text = 过滤文章?[indexPath.row].title
cell.newsImage.downloadImage(来自:(self.filteredArticles?[indexPath.item].urlImage ?? "nill"))
cell.timePublication.text = filteredArticles?[indexPath.row].publishedAt
如果让 dateString = filtersArticles?[indexPath.row].publishedAt,
让日期 = indDateFormatter.date(来自:dateString){
让 formattedString = outDateFormtter.string(from: date)
cell.timePublication.text = 格式化字符串
} 别的 {
cell.timePublication.text = "----------"
}
返回单元格
}
func updateSearchResults(for searchController: UISearchController) {
让 searchBar = searchController.searchBar
filterContentForSearchText(searchBar.text!,文章!)
}
func filterContentForSearchText(_ searchText:String ,_ category: [文章]){
filtersArticles = 文章?.filter({ (article:Articles) -> Bool in
返回 article.description.lowercased().contains(searchText.lowercased())
})
table_view.reloadData()
}
【问题讨论】: