close

Show image and label if row count is zero in UITableView using swift 3

QuestionsCategory: SwiftShow image and label if row count is zero in UITableView using swift 3
Taimoor asked 4 months ago

When my tableView’s row count is zero I am successfully display a “No Result Label“. But I want to display an image above the UILabel in “numberOfRowsInSection”. How can I do that in swift 3 please.

SorAtrore replied 2 weeks ago

thesis formatting service
sample dissertation topics
what is a doctoral dissertation
business dissertation
dissertation proposal service

custom dissertation writing services
dissertation proposal samples

professional dissertation writers
dissertation proposal sample

write my dissertation for me
apa dissertation template

what is a phd thesis
dissertation order

write my dissertation uk
professional dissertation writers

affordable dissertation writing services
affordable dissertation writing services

dissertation proposal format
teaching thesis statements

custom thesis writing services
sample dissertation

dissertation format apa
phd how to write
dissertation proposal template
what is a thesis
buy a thesis paper
dissertation format

dissertation apa format
dissertation topics in marketing
how to start thesis writing
phd editing services
phd accounting thesis topics
thesis statement examples

thesis formatting service
dissertation service reviews

what is a phd proposal
dissertation editing services

cheap dissertation help
sample of dissertation proposal

best thesis writing services
professional thesis writing service

best thesis writing service
write to them

1 Answers
Best Answer
Chaudhary Talha Staff answered 4 months ago

You need a to make a custom UIView and then give it the backgroundView of your UITableView. Imagine the image size is 50×50 so your methods will go like:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 50, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
        
        let image = UIImage(named: "noData")
        let noDataImage = UIImageView(image: image)
        
        noDataImage.contentMode = UIViewContentMode.scaleAspectFit
        
        noDataImage.frame = CGRect(x: tableView.center.x-25, y: 0, width: 50, height: tableView.bounds.size.height)

        let backgroundView = UIView(frame: CGRect(x:0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
        
        if allData.count == 0 {
            
            noDataLabel.isHidden = false
            noDataImage.isHidden = false

            noDataLabel.text          = "No data available. Add new entry \nby pressing the + icon on top right."
            noDataLabel.textColor     = UIColor.black
            noDataLabel.numberOfLines = 3
            noDataLabel.backgroundColor = UIColor.white
            noDataLabel.textAlignment = .center
            backgroundView.addSubview(noDataLabel)
            backgroundView.addSubview(noDataImage)
            
            tableView.backgroundView=backgroundView

            tableView.separatorStyle  = .none
            return 0;
        }
        else {
            noDataLabel.backgroundColor = UIColor.clear
            noDataLabel.isHidden = true
            noDataImage.isHidden = true
            tableView.backgroundView  = nil
            return allData.count
        }

    }