Tarvo Mäesepp Tarvo Mäesepp - 28 days ago 10
iOS Question

Show label and image if tableview is empty

I created solution when tableView is empty then show label "No data available" else show content. However I am not able to show background image AND label together. What I am doing wrong?

This is how I do it:

var numOfSections: Int = 0
if reviewsArray.count != 0{

tableView.separatorStyle = .singleLine
numOfSections = reviewsArray.count

}
else{
let noDataLabel: UILabel = UILabel(frame: CGRect(0, 0, tableView.bounds.size.width, tableView.bounds.size.height))
noDataLabel.text = "No data available"
noDataLabel.textColor = UIColor.black
noDataLabel.textAlignment = .center
tableView.backgroundView = noDataLabel
tableView.separatorStyle = .none

}


And in viewDidLoad I add background image with blur:

let blurredBackgroundView = BlurredBackgroundView(frame: .zero)
tableView.backgroundView = blurredBackgroundView
tableView.separatorEffect = UIVibrancyEffect(blurEffect: blurredBackgroundView.blurView.effect as! UIBlurEffect)


Please do not suggest any third party library :)

Answer

You are setting the background view to blurredBackgroundView, and then to noDataLabel. By setting it to noDataLabel, you are getting rid of blurredBackgroundView.

I'd try adding noDataLabel as a subview of the background view. Like:

tableView.backgroundView.addSubview(noDataLabel)

Comments