Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to check if TableView is empty

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 ;)

like image 807
Jenoah Kers Avatar asked Apr 27 '16 15:04

Jenoah Kers


People also ask

How do you clear a Tableview in Swift?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

What is Tableview in Swift?

Overview. Table views in iOS display rows of vertically scrolling content in a single column. Each row in the table contains one piece of your app's content. For example, the Contacts app displays the name of each contact in a separate row, and the main page of the Settings app displays the available groups of settings ...

What is Tableview datasource in Swift?

The methods that an object adopts to manage data and provide cells for a table view.

What is indexPath in Tableview Swift?

indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.


1 Answers

In Swift 3:

if tableView.visibleCells.isEmpty {
    //tableView is empty. You can set a backgroundView for it.
} else {
    //do something
}
like image 196
J.Arji Avatar answered Nov 04 '22 06:11

J.Arji