Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.cancelState = 0;
- // Uncomment the following line to preserve selection between presentations.
- // self.clearsSelectionOnViewWillAppear = NO;
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem;
- self.dataTable.delegate = self;
- self.dataTable.dataSource = self;
- //NSLog(@"widgets is %@",self.searchElement);
- //
- NSString *version = [[UIDevice currentDevice] systemVersion];
- int ver = [version intValue];
- if (ver < 7){
- //iOS 6 work// Nav bar
- searchBar.hidden = YES;
- [navBar setBackgroundImage:[[UIImage imageNamed:@"top6.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 5, 10, 5)] forBarMetrics:UIBarMetricsDefault];
- navBar.frame = CGRectMake(0, 0, 320, 44);
- searchBar6 = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 90, 320, 44)];
- searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar6 contentsController:self];
- searchDisplayController.delegate = self;
- searchDisplayController.searchResultsDataSource = self;
- [self.view addSubview:searchBar6];
- UIImage* image = [UIImage imageNamed:@"hammenu.png"];
- CGRect frame = CGRectMake(-10, 0, image.size.width, image.size.height);
- UIButton* someButton = [[UIButton alloc] initWithFrame:frame];
- [someButton addTarget:self action:@selector(rightSideMenuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
- [someButton setBackgroundImage:image forState:UIControlStateNormal];
- [someButton setShowsTouchWhenHighlighted:NO];
- UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithCustomView:someButton];
- [self.navigationItem setRightBarButtonItem:button];
- }
- else{
- searchBar.hidden = NO;
- //to add navigation blue color
- [navBar setBackgroundImage:[UIImage imageNamed:@"top.png"] forBarMetrics:UIBarMetricsDefault];
- }
- // Test if data is there
- self.mmqc = [[MMMQueryControl alloc] init];
- self.dbData = [[NSMutableArray alloc] init];
- self.searchResults = [[NSMutableArray alloc] init];
- self.locationData =[[NSMutableArray alloc] init];
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
- NSEntityDescription *entity = [NSEntityDescription entityForName:self.searchElement inManagedObjectContext:[self managedObjectContext]];
- [fetchRequest setEntity:entity];
- NSError *error = nil;
- NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
- if (fetchedObjects == nil)
- {
- //NSLog(@"Problem !! %@",error);
- }
- if ([self.searchElement isEqualToString:@"Room"]) {
- for (Room *c in fetchedObjects) {
- NSString *tableStr = [NSString stringWithFormat:@"%@ (%@)",c.roomName,[self.mmqc fetchLocationForRoomID:[c.roomID integerValue]]];
- [self.dbData addObject:tableStr];
- //NSLog(@"RoomNum is %@ by roomID %@", c.roomName,c.roomID);
- //NSLog(@"LocationID is %@",[self.mmqc fetchLocationForRoomID:[c.roomID integerValue]]);
- [self.locationData addObject:[self.mmqc fetchLocationForRoomID:[c.roomID integerValue]]];
- }
- }
- else if ([self.searchElement isEqualToString:@"Cleaner"]){
- for (Cleaner *c in fetchedObjects) {
- [self.dbData addObject:c.cleanerName];
- }
- }
- }
- - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
- return UIBarPositionTopAttached;
- }
- - (NSManagedObjectContext *)managedObjectContext
- {
- NSManagedObjectContext *contex = nil;
- id delegate = [[UIApplication sharedApplication]delegate];
- if ([delegate performSelector:@selector(managedObjectContext)]) {
- contex = [delegate managedObjectContext];
- }
- return contex;
- }
- - (NSManagedObjectModel *)managedObjectModel
- {
- NSManagedObjectModel *model = nil;
- id delegate = [[UIApplication sharedApplication]delegate];
- if ([delegate performSelector:@selector(managedObjectModel)]) {
- model = [delegate managedObjectModel];
- }
- return model;
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [self.navigationController setNavigationBarHidden:NO animated:animated];
- [super viewWillAppear:animated];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Cancel Btn
- -(IBAction)cancelAction:(id)sender{
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- { // Return the number of sections.
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- // Return the number of rows in the section.
- if (tableView == self.searchDisplayController.searchResultsTableView) {
- return [self.searchResults count];
- } else {
- return [self.dbData count];
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
- // Configure the cell...
- NSLog(@"self.searchDisplayController.active is %d",self.searchDisplayController.active);
- if (tableView == self.searchDisplayController.searchResultsTableView) {
- NSLog(@"Search Result is %@",[self.searchResults objectAtIndex:indexPath.row]);
- cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
- } else {
- cell.textLabel.text =[self.dbData objectAtIndex:indexPath.row];
- //cell.textLabel.text = [NSString stringWithFormat:@"%@",c.houseKeeper];//c.roomNum;
- }
- return cell;
- }
- - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
- {
- NSPredicate *resultPredicate = [NSPredicate
- predicateWithFormat:@"SELF beginswith[cd] %@",
- searchText];
- self.searchResults = [[self.dbData filteredArrayUsingPredicate:resultPredicate] mutableCopy];
- if (self.searchResults.count>0) {
- self.searchDisplayController.active =YES;
- }
- }
- #pragma mark - UISearchDisplayController delegate methods
- -(BOOL)searchDisplayController:(UISearchDisplayController *)controller
- shouldReloadTableForSearchString:(NSString *)searchString
- {
- [self filterContentForSearchText:searchString
- scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
- objectAtIndex:[self.searchDisplayController.searchBar
- selectedScopeButtonIndex]]];
- self.searchString = searchString;
- return YES;
- }
- -(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
- {
- [self.dataTable reloadData];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
- self.selectedString = selectedCell.textLabel.text;
- self.cancelState = 1;
- NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
- [storeData setObject:self.selectedString forKey:@"Key"];
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- -(IBAction)addAction:(id)sender{
- BOOL success = [self.mmqc storeCleaner:self.searchString];
- if (success) {
- // NSString *alertStr = [NSString stringWithFormat:@"Successfully Added:%@",self.searchString];
- //
- // UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:nil message:alertStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
- NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
- [storeData setObject:self.searchString forKey:@"Key"];
- [self dismissViewControllerAnimated:YES completion:nil];
- //[alrt show];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement