2016-04-27 19 views
0

だから私はこのToDoリストアプリを作っています。 このアプリはローカル通知を持っていますが、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

:テーブルビューが空の場合

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

@ジェノアあなたの問題を最もよく解決した答えを受け入れることを忘れないでください。 – 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

...テーブルビュー空の場合。

データソース配列で呼び出される同じ名前のブール値のプロパティがあります。

配列に要素が含まれていない場合はtrueです。

taskMgr.tasks.isEmpty 
0

他の回答に記載されているように、最も良い方法は、データのカウントで確認することです。しかし、あなたは他の方法で確認したい場合は、あなたが使用することができます。

スウィフト3では
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; 
} 
関連する問題