ios - 未找到搜索结果时添加带有标签的背景视图
问题描述
当没有找到搜索结果时,我正在尝试添加背景视图。我已经阅读了这个网站上的几个问题/答案,但没有一个有效。我希望背景视图在结果计数等于 0 时显示“无搜索结果”。
这是我尝试过的:
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()
}
}
解决方案
我想你是在运行中设置的backgroundView
,你可以试试
self.filmsTable.backgroundView = emptyLabel
如果 name 是也更有意义filmsCollection
推荐阅读
- python - 无法从事件列表站点过滤元素字符串
- reactjs - 模拟 Axios 多个请求
- android - 使用 vscode 调试器调试 React Native
- reactjs - 异步等待 redux this.props.func 调用?
- python - 初始化要在 SelectField 中使用的 Flask WTFForm 类变量
- hadoop - 目录文件未使用命令“hadoop fs -put”复制到 HDFS
- ubuntu - 在 Ubuntu 18.04 中启动 Xampp 时遇到 MySQL 错误
- c# - Linq查询不使用表的对象获取表的记录
- android - Unity游戏中出现广告时如何重启手机?
- flutter - Flutter v1.0下的类TickerProviderStateMixin undefined