【发布时间】:2019-09-01 13:32:51
【问题描述】:
当我的TableView 为空时,我使用此功能添加背景视图。
func emptyTableViewMessage(with message: String) {
let messageLabel: UILabel = UILabel(frame: CGRect(x: todoTableView.bounds.size.width/2, y: todoTableView.bounds.size.height / 2 , width: todoTableView.bounds.size.width, height: todoTableView.bounds.size.height))
messageLabel.text = message
messageLabel.textColor = UIColor.gray
messageLabel.font = UIFont(name: "Open Sans", size: 15)
messageLabel.textAlignment = .center
todoTableView.backgroundView = messageLabel
}
我叫它numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
if searchedData?.count == 0 {
emptyTableViewMessage(with: "Not found")
} else {
emptyTableViewMessage.backgroundView = .none
}
return (searchedData?.count)!
}
但是如果我有几个部分,例如其中一个是空的,我怎样才能在部分内添加这种背景视图(不是在标题中,因为我在标题中有不同的项目)
非常感谢
【问题讨论】:
-
numberOfRowsInSection被多次调用。那不是调用emptyTableViewMessage方法的地方。在实际加载数据时调用它,而不是从任何数据源方法中调用。