我已经研究了很多答案,但要么只是 UILabel
要么是 UIImage
而不是两者。所以在尝试实现之后我终于发现我们不能做两个tableView.backgroundView
。这是我到目前为止所做的:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
let image = UIImage(named: "noData")
let noDataImage = UIImageView(image: image)
noDataImage.frame = CGRect(x: 0, y: 10, width: 20, height: 20)
if allData.count == 0 {
noDataLabel.isHidden = false
noDataImage.isHidden = false
noDataLabel.text = "No data added. Add new entry \nby pressing the add icon on top right."
noDataLabel.textColor = UIColor.black
noDataLabel.numberOfLines = 3
noDataLabel.backgroundColor = UIColor.white
noDataLabel.textAlignment = .center
//what to do from here
tableView.backgroundView = noDataImage
tableView.backgroundView = noDataLabel
//end
tableView.separatorStyle = .none
return 0;
}
else {
noDataLabel.backgroundColor = UIColor.clear
noDataLabel.isHidden = true
noDataImage.isHidden = true
tableView.backgroundView = nil
return allData.count
}
我想显示一张图片,我想在该图片下方显示一个 UILabel
。我怎样才能做到这一点?
最佳答案
您需要创建一个包含 subview
、您的图像
和标签
的 View
var backgroundView =UIView(frame: CGRectMake(0, 0, your_width, your_height))
backgroundView.addSubview(noDataImage)
backgroundView.addSubview(noDataLabel)
tableView.backgroundView=backgroundView;
注意:根据您的使用调整noDataImage
和noDataLabel
的框架。
关于ios - 如果 UITableView 没有数据,如何添加 UIImage 和 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44521464/