                 })
             }
         }
+        
+    }
+    
+    // called when cancel button pressed
+    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
+        print(#function)
+        self.searchBar.showsCancelButton = false
+        self.searchBar.resignFirstResponder()
+        self.searchBar.text = ""
+        self.loadData()
+    }
+    
+    
+    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
+        self.searchBar.showsCancelButton = true
+        return true
+    }
+    
+    
+    //MARK: - LoadData
+    private func loadData() {
+        autoreleasepool {
+            var error: NSError? = nil
+            var result: [AnyObject]?
+            
+            let fetch: NSFetchRequest = NSFetchRequest(entityName: "SubCategory")
+            
+            do {
+                result = try self.managedObjectContext!.executeFetchRequest(fetch)
+            } catch let nserror1 as NSError{
+                error = nserror1
+                result = nil
+            }
+            if result != nil {
+                self.data = []
+                self.data = (result as! [SubCategory]).filter({ (sc) -> Bool in
+                    !sc.bills.isEmpty
+                })
+            }
+        }
     }
 }
        return UIEdgeInsetsMake(10, 5, 10, 5); //top,left,bottom,right
    }
    
    //MARK:- LoadData
    private func loadData() {
        autoreleasepool {
            var error: NSError? = nil
            var result: [AnyObject]?
            
            let fetch: NSFetchRequest = NSFetchRequest(entityName: "SubCategory")
            
            do {
                result = try self.managedObjectContext!.executeFetchRequest(fetch)
            } catch let nserror1 as NSError{

    }
    
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        if kind == UICollectionElementKindSectionHeader && !self.data[indexPath.section].bills.isEmpty {
            let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: reuseIdentifierHeader, forIndexPath: indexPath) as! BillHeaderView
            header.title.text = self.data[indexPath.section].name
            return header;
        }
        return UICollectionReusableView()
    }
    
