Прежде всего, не ставьте showNoDataTableView()
в numberOfRowsInSection
. Просто верните обычный
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
и укажите showNoDataTableView()
в асинхронном закрытии отправки в loadMessages()
DispatchQueue.main.async {
self.tableView.reloadData()
showNoDataTableView()
}
В showNoDataTableView()
вы должны проверить
- Создайте метку, если
messages
пуст, а метка не существует (пока).
- Удалите метку, если
messages
равен , а не пуст и метка существует .
В обоих остальных случаях ничего не делать.
func showNoDataTableView() {
// self.activityIndicator.stopAnimating()
if messages.isEmpty && tableView.backgroundView == nil {
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width,
height: self.tableView.bounds.size.height))
noDataLabel.numberOfLines = 0
noDataLabel.text = "No messages yet :( \r\n\n Don't be afraid to start a conversation."
noDataLabel.textColor = Theme.current.label_noData_textColor
noDataLabel.font = Theme.current.label_noData_font
noDataLabel.textAlignment = .center
tableView.backgroundView = noDataLabel
tableView.backgroundView?.backgroundColor = Theme.current.tableView_backgroundView_backgroundColor
tableView.separatorStyle = .none
} else if !messages.isEmpty && tableView.backgroundView != nil {
tableView.backgroundView = nil
tableView.separatorStyle = // set default style
}
}