我已经研究过很多答案,但无论是它的唯一UILabel
或UIImage
不能同时使用。所以在试图实现它后,我终于发现我们不能做两个tableView.backgroundView
。以下是我迄今所做的:如何添加的UIImage和的UILabel如果UITableView中没有数据
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
。我怎样才能做到这一点?
我会把上面的tableView你的“无数据”的观点,使tableViews背景透明。 – shallowThought
你的意思是对的UIView我要补充的图像和标签,如果有计数为零,那么我应该做表格的背景透明? –
创建一个视图比ASIGN到背景来看,这已经被通过以下@arpit回答 – Dhiru