2016-04-27 148 views
0

所以我正在制作这个ToDo-list应用程序。 这个程序有本地通知,但我只希望他们弹出如果tableview为空。保持简短:如何检查tableview是否为空?Swift - 如何检查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() 
    } 

} 

任何人谁可以帮我吗? 谢谢;)

+4

你不检查表视图是空的。您检查数据源中是否有任何数据。 – rmaddy

回答

2

在斯威夫特3:

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

很简单的答案! – user7097242

5

您应该检查taskMgr.tasks.count值。

+0

感谢您的快速反应,它的工作原理!谢谢 – Jenoah

+0

@Jenoah不要忘记接受最能解决您问题的答案。 – rmaddy

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

..如果TableView中是空

存在一个具有相同名称的布尔属性,将在数据源数组上调用。如果数组不包含元素,则它是true

taskMgr.tasks.isEmpty 
0

正如在其他答案中提到的,最好的方法是检查您的数据计数。但是如果你想检查与任何其他方式,你可以使用:

if tableView.visibleCells.count == 0 { 
     // tableView is empty. You can set a backgroundView for it. 
     let label = UILabel(frame: CGRectMake(0, 0, tableView.bounds.size.width, tableView.bounds.size.height)) 
     label.text = "No Data" 
     label.textColor = UIColor.blackColor(); 
     label.TextAlignment = .Center 
     label.sizeToFit() 
     tableView.backgroundView = label; 
     tableView.separatorStyle = .None; 
}