2017-06-13 21 views
0

我已经研究过很多答案,但无论是它的唯一UILabelUIImage不能同时使用。所以在试图实现它后,我终于发现我们不能做两个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。我怎样才能做到这一点?

+0

我会把上面的tableView你的“无数据”的观点,使tableViews背景透明。 – shallowThought

+0

你的意思是对的UIView我要补充的图像和标签,如果有计数为零,那么我应该做表格的背景透明? –

+0

创建一个视图比ASIGN到背景来看,这已经被通过以下@arpit回答 – Dhiru

回答

4

你需要创建一个viewsubviewsimagelabel

var backgroundView =UIView(frame: CGRectMake(0, 0, your_width, your_height)) 
backgroundView.addSubview(noDataImage) 
backgroundView.addSubview(noDataLabel) 

tableView.backgroundView=backgroundView; 

注:调整noDataImagenoDataLabel帧,按您的使用。

0
backgroundView Property 

表视图的背景视图。

宣言

斯威夫特

VAR backgroundView:UIView的?

目标C

@属性(非原子,读写,保留)的UIView * backgroundView

讨论

甲表视图的背景视图会自动调整大小以匹配表视图的大小。此视图作为所有单元格,标题视图和页脚视图后面的表视图的子视图放置。

您必须将此属性设置为无设置表视图的背景颜色。

相关问题