user5513630
user5513630

Reputation: 1699

Show no data in table view - when no data return from api

I have some 15 collection view cell.when user click each cell, that respective cell data will display in next screen table view.But , some cell are don't have any data.In that case i need to shoe in table view that "No Data".How to show that??

here is my code:

These are the delegate method in my table view :

 // array to store the value from json
    var arrDict = [Businessdata]()



    func numberOfSectionsInTableView(tableView: UITableView) -> Int
        {
            return 1
        }

        // number of rows
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {
            return self.arrDict.count

        }


        // calling each cell based on tap and users ( premium / non premium )
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {


            //let cell:customCell = self.TableViewList.dequeueReusableCellWithIdentifier("cell") as! customCell
            let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell") as! customCell

            cell.vendorName.text = arrDict[indexPath.row].BusinessName
            cell.vendorAddress.text = arrDict[indexPath.row].Address
            cell.VendorRating.rating = arrDict[indexPath.row].Rating!

            return cell
        }

Please help me out, where i have to declare that .I am new to ios development.Thanks !

Upvotes: 2

Views: 3603

Answers (2)

Muhammad Kashif
Muhammad Kashif

Reputation: 9

Use the following extension

extension UITableView {
    
    func setEmptyMessage(_ message: String) {
        let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
        messageLabel.text = message
        messageLabel.textColor = .black
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = .center
        messageLabel.sizeToFit()
        
        self.backgroundView = messageLabel
        self.separatorStyle = .none
    }
    
    func restore() {
        self.backgroundView = nil
        self.separatorStyle = .none
    }
}

Use it in Table View items in row delegate function as

if your_list.count == 0 {
    self.tableView.setEmptyMessage("There is no data")
} else {
    self.tableView.restore()
}
return your_list.count

Upvotes: 1

Mr. Bond
Mr. Bond

Reputation: 427

Try this:

func numberOfSectionsInTableView(tableView: UITableView) -> Int 
{

        var numOfSection: NSInteger = 0

        if YourArraydata.count > 0 
         {

            self.tableView.backgroundView = nil
            numOfSection = 1


         } 
         else
         {

            var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
            noDataLabel.text = "No Data Available"
            noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
            noDataLabel.textAlignment = NSTextAlignment.Center
            self.tableView.backgroundView = noDataLabel

          }

        return numOfSection
  }

enter image description here

Upvotes: 4

Related Questions