         
         self.definesPresentationContext = true
     }
-
+    
+    override func viewWillAppear(animated: Bool) {
+        for scheduleCell in tableView.visibleCells as! [ScheduleTableViewCell] {
+            guard let indexPath = tableView.indexPathForCell(scheduleCell) else {
+                return
+            }
+            
+            var scheduleItem: Schedule? = nil
+            
+            if searchController.active && searchController.searchBar.text != "" {
+                scheduleItem = filteredScheduleItems[indexPath.row]
+            } else if scheduleItems[indexPath.section].count > indexPath.row {
+                scheduleItem = scheduleItems[indexPath.section][indexPath.row]
+            }
+            
+            if let foundScheduleItem = scheduleItem {
+                scheduleCell.starButton.selected = foundScheduleItem.starred
+            }
+        }
+        
+        super.viewWillAppear(animated)
+    }
+    
+    
+    
+    override func viewDidAppear(animated: Bool) {
+        if tableView.numberOfRowsInSection(0) > 0 {
+            scrollToNearestCell()
+        }
+        
+        super.viewDidAppear(animated)
+    }
+    
     func scrollToNearestCell() {
         if searchController.active && searchController.searchBar.text != "" {
             self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: true)
             
             return
         }
-
+        
         var lastPath = NSIndexPath(forRow: 0, inSection: 0)
-        let oneHourAgo = NSDate(timeIntervalSinceNow: -(1*60*60))
+        let oneHourAgo = NSDate(timeIntervalSinceNow: -(1*60*60)) // NSDate(timeIntervalSinceNow: 4*24*60*60) // test by adding 4 days
         
         for (section, sectionArray) in scheduleItems.reverse().enumerate() {
             for (row, scheduleItem) in sectionArray.reverse().enumerate() {

        
        return scheduleItems[section].count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ScheduleCell", forIndexPath: indexPath)

        guard let scheduleCell = cell as? ScheduleTableViewCell else {
            return cell
        }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if DataStore.sharedInstance.scheduleItems.count == 0 {
            DataStore.sharedInstance.updateScheduleItems() { _ in
                self.tableView.reloadData()
                
                self.sortScheduleItems(starredOnly: false)
                
                self.scrollToNearestCell()
            }
        } else {
            sortScheduleItems(starredOnly: false)
            
            scrollToNearestCell()
        }
        
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        
        searchController.searchBar.tintColor = UIColor.lightCharcoal()
        searchController.searchBar.delegate = self
        
