Swift - How to check if TableView is empty

12,057

Solution 1

In Swift 3:

if tableView.visibleCells.isEmpty {
    //tableView is empty. You can set a backgroundView for it.
} else {
    //do something
}

Solution 2

You should check taskMgr.tasks.count value.

Solution 3

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if taskMgr.tasks.count == 0 {
       //table view is empty here
    }
    return taskMgr.tasks.count    
}

Solution 4

.. if TableView is empty.

There is a boolean property with the same name , to be called on the data source array.

It's true if the array contains no elements.

taskMgr.tasks.isEmpty
Share:
12,057
Jenoah Kers
Author by

Jenoah Kers

Updated on June 18, 2022

Comments

  • Jenoah Kers
    Jenoah Kers almost 2 years

    So i'm making this ToDo-list app. This app has local notifications, but i only want them to pop-up if the tableview is empty. To keep it short : How do i check if the tableview is empty?

    This is my current code :

    import UIKit
    
    class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    @IBOutlet var tblTasks : UITableView!
    @IBOutlet weak var countLbl: UILabel!
     var localNotification = UILocalNotification()
    
    //For persisting data
    let defaults = NSUserDefaults.standardUserDefaults()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tblTasks.reloadData()
    
    
        // localNotification.alertAction = "Je hebt nog taken die gedaan moeten worden!"
        localNotification.alertBody = "Je hebt nog taken die gedaan moeten worden! Namelijk nog \(updateCount)"
        localNotification.timeZone = NSTimeZone.localTimeZone()
    
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    
    }
    
    override func viewWillAppear(animated: Bool) {
        self.tblTasks.reloadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return taskMgr.tasks.count
    
    }
    
    //Define how our cells look - 2 lines a heading and a subtitle
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")
    
        //Assign the contents of our var "items" to the textLabel of each cell
        cell.textLabel!.text = taskMgr.tasks[indexPath.row].name
        cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].description
        cell.backgroundColor = UIColor.clearColor()
    
        return cell
    
    }
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
    
        if (editingStyle == UITableViewCellEditingStyle.Delete){
    
            taskMgr.removeTask(indexPath.row)
            tblTasks.reloadData()
        }
    
    }
    

    Anyone who can help me? Thanks ;)

  • rmaddy
    rmaddy about 8 years
    @Jenoah Don't forget to accept the answer that best solved your issue.
  • Vinu David Jose
    Vinu David Jose almost 5 years
    This one is deprecated in iOS 13. we cant check this while they were in the process of being updated