Swift - 如何检查TableView是否为空

Jen*_*oah 5 uitableview tableview ios swift

所以我正在制作这个待办事项列表应用程序.这个应用程序有本地通知,但我只希望它们弹出如果tableview为空.保持简短:如何检查tableview是否为空?

这是我目前的代码:

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()
    }

}
Run Code Online (Sandbox Code Playgroud)

谁可以帮助我?谢谢 ;)

J.A*_*rji 17

在Swift 3中:

if tableView.visibleCells.isEmpty {
    //tableView is empty. You can set a backgroundView for it.
} else {
    //do something
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*kov 5

你应该检查taskMgr.tasks.count价值.