找不到任何搜索结果时,我试图添加一个backgroundview。我已经阅读了本网站上的几个问题/答案,但没有一个起作用。我希望当结果计数等于0时,backgroundview显示“无搜索结果”。
这是我尝试过的:
var filmsTable: UICollectionView!
var backgroundView: UIView?
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
self.filteredFilms = films
self.filmsTable.reloadData()
} else {
self.filteredFilms = films!.filter({(($0["title"] as! String).lowercased().contains(searchText.lowercased()))})
//print(self.filteredFilms!.count)
if self.filteredFilms!.count == 0 {
let emptyLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.filmsTable.frame.width, height: self.filmsTable.frame.height))
emptyLabel.text = "No Search Results"
emptyLabel.textColor = .black
emptyLabel.numberOfLines = 0;
emptyLabel.textAlignment = .center;
emptyLabel.font = UIFont(name: "Times", size: 18)
emptyLabel.sizeToFit()
self.backgroundView = emptyLabel;
}
self.filmsTable.reloadData()
}
}
答案 0 :(得分:1)
我认为您可以随时设置backgroundView
,可以尝试
self.filmsTable.backgroundView = emptyLabel
如果名称为filmsCollection