Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. self.cancelState = 0;
  5. // Uncomment the following line to preserve selection between presentations.
  6. // self.clearsSelectionOnViewWillAppear = NO;
  7.  
  8. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  9. // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  10.  
  11. self.dataTable.delegate = self;
  12. self.dataTable.dataSource = self;
  13.  
  14. //NSLog(@"widgets is %@",self.searchElement);
  15. //
  16.  
  17. NSString *version = [[UIDevice currentDevice] systemVersion];
  18.  
  19. int ver = [version intValue];
  20. if (ver < 7){
  21. //iOS 6 work// Nav bar
  22. searchBar.hidden = YES;
  23. [navBar setBackgroundImage:[[UIImage imageNamed:@"top6.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 5, 10, 5)] forBarMetrics:UIBarMetricsDefault];
  24. navBar.frame = CGRectMake(0, 0, 320, 44);
  25.  
  26. searchBar6 = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 90, 320, 44)];
  27. searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar6 contentsController:self];
  28.  
  29. searchDisplayController.delegate = self;
  30. searchDisplayController.searchResultsDataSource = self;
  31.  
  32. [self.view addSubview:searchBar6];
  33.  
  34.  
  35. UIImage* image = [UIImage imageNamed:@"hammenu.png"];
  36. CGRect frame = CGRectMake(-10, 0, image.size.width, image.size.height);
  37. UIButton* someButton = [[UIButton alloc] initWithFrame:frame];
  38. [someButton addTarget:self action:@selector(rightSideMenuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  39. [someButton setBackgroundImage:image forState:UIControlStateNormal];
  40. [someButton setShowsTouchWhenHighlighted:NO];
  41. UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithCustomView:someButton];
  42. [self.navigationItem setRightBarButtonItem:button];
  43.  
  44.  
  45. }
  46. else{
  47. searchBar.hidden = NO;
  48. //to add navigation blue color
  49. [navBar setBackgroundImage:[UIImage imageNamed:@"top.png"] forBarMetrics:UIBarMetricsDefault];
  50. }
  51. // Test if data is there
  52. self.mmqc = [[MMMQueryControl alloc] init];
  53. self.dbData = [[NSMutableArray alloc] init];
  54. self.searchResults = [[NSMutableArray alloc] init];
  55. self.locationData =[[NSMutableArray alloc] init];
  56.  
  57. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  58. NSEntityDescription *entity = [NSEntityDescription entityForName:self.searchElement inManagedObjectContext:[self managedObjectContext]];
  59. [fetchRequest setEntity:entity];
  60.  
  61. NSError *error = nil;
  62. NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
  63.  
  64. if (fetchedObjects == nil)
  65. {
  66. //NSLog(@"Problem !! %@",error);
  67. }
  68. if ([self.searchElement isEqualToString:@"Room"]) {
  69. for (Room *c in fetchedObjects) {
  70. NSString *tableStr = [NSString stringWithFormat:@"%@ (%@)",c.roomName,[self.mmqc fetchLocationForRoomID:[c.roomID integerValue]]];
  71. [self.dbData addObject:tableStr];
  72.  
  73. //NSLog(@"RoomNum is %@ by roomID %@", c.roomName,c.roomID);
  74. //NSLog(@"LocationID is %@",[self.mmqc fetchLocationForRoomID:[c.roomID integerValue]]);
  75. [self.locationData addObject:[self.mmqc fetchLocationForRoomID:[c.roomID integerValue]]];
  76.  
  77. }
  78. }
  79.  
  80. else if ([self.searchElement isEqualToString:@"Cleaner"]){
  81. for (Cleaner *c in fetchedObjects) {
  82.  
  83. [self.dbData addObject:c.cleanerName];
  84.  
  85. }
  86. }
  87. }
  88.  
  89. - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
  90. return UIBarPositionTopAttached;
  91. }
  92.  
  93. - (NSManagedObjectContext *)managedObjectContext
  94. {
  95. NSManagedObjectContext *contex = nil;
  96.  
  97. id delegate = [[UIApplication sharedApplication]delegate];
  98.  
  99. if ([delegate performSelector:@selector(managedObjectContext)]) {
  100. contex = [delegate managedObjectContext];
  101. }
  102. return contex;
  103. }
  104.  
  105. - (NSManagedObjectModel *)managedObjectModel
  106. {
  107. NSManagedObjectModel *model = nil;
  108.  
  109. id delegate = [[UIApplication sharedApplication]delegate];
  110.  
  111. if ([delegate performSelector:@selector(managedObjectModel)]) {
  112. model = [delegate managedObjectModel];
  113. }
  114.  
  115. return model;
  116. }
  117.  
  118.  
  119. - (void)viewWillAppear:(BOOL)animated
  120. {
  121. [self.navigationController setNavigationBarHidden:NO animated:animated];
  122. [super viewWillAppear:animated];
  123. }
  124.  
  125. - (void)didReceiveMemoryWarning
  126. {
  127. [super didReceiveMemoryWarning];
  128. // Dispose of any resources that can be recreated.
  129. }
  130.  
  131. #pragma mark - Cancel Btn
  132.  
  133. -(IBAction)cancelAction:(id)sender{
  134.  
  135. [self dismissViewControllerAnimated:YES completion:nil];
  136.  
  137. }
  138.  
  139. #pragma mark - Table view data source
  140.  
  141. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  142. { // Return the number of sections.
  143. return 1;
  144. }
  145.  
  146. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  147. {
  148. // Return the number of rows in the section.
  149. if (tableView == self.searchDisplayController.searchResultsTableView) {
  150. return [self.searchResults count];
  151.  
  152. } else {
  153. return [self.dbData count];
  154.  
  155. }
  156. }
  157.  
  158. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  159. {
  160. static NSString *CellIdentifier = @"Cell";
  161. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  162.  
  163. if (cell == nil) {
  164. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  165.  
  166. }
  167. // Configure the cell...
  168. NSLog(@"self.searchDisplayController.active is %d",self.searchDisplayController.active);
  169. if (tableView == self.searchDisplayController.searchResultsTableView) {
  170.  
  171. NSLog(@"Search Result is %@",[self.searchResults objectAtIndex:indexPath.row]);
  172. cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
  173. } else {
  174. cell.textLabel.text =[self.dbData objectAtIndex:indexPath.row];
  175. //cell.textLabel.text = [NSString stringWithFormat:@"%@",c.houseKeeper];//c.roomNum;
  176. }
  177.  
  178. return cell;
  179. }
  180.  
  181. - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
  182. {
  183. NSPredicate *resultPredicate = [NSPredicate
  184. predicateWithFormat:@"SELF beginswith[cd] %@",
  185. searchText];
  186.  
  187. self.searchResults = [[self.dbData filteredArrayUsingPredicate:resultPredicate] mutableCopy];
  188. if (self.searchResults.count>0) {
  189. self.searchDisplayController.active =YES;
  190. }
  191.  
  192. }
  193.  
  194. #pragma mark - UISearchDisplayController delegate methods
  195. -(BOOL)searchDisplayController:(UISearchDisplayController *)controller
  196. shouldReloadTableForSearchString:(NSString *)searchString
  197. {
  198. [self filterContentForSearchText:searchString
  199. scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
  200. objectAtIndex:[self.searchDisplayController.searchBar
  201. selectedScopeButtonIndex]]];
  202.  
  203. self.searchString = searchString;
  204.  
  205.  
  206.  
  207. return YES;
  208. }
  209.  
  210. -(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
  211. {
  212. [self.dataTable reloadData];
  213. }
  214.  
  215. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  216. {
  217. UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
  218. self.selectedString = selectedCell.textLabel.text;
  219. self.cancelState = 1;
  220.  
  221. NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
  222. [storeData setObject:self.selectedString forKey:@"Key"];
  223. [self dismissViewControllerAnimated:YES completion:nil];
  224. }
  225.  
  226. -(IBAction)addAction:(id)sender{
  227.  
  228. BOOL success = [self.mmqc storeCleaner:self.searchString];
  229.  
  230. if (success) {
  231. // NSString *alertStr = [NSString stringWithFormat:@"Successfully Added:%@",self.searchString];
  232. //
  233. // UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:nil message:alertStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  234.  
  235. NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
  236. [storeData setObject:self.searchString forKey:@"Key"];
  237. [self dismissViewControllerAnimated:YES completion:nil];
  238.  
  239. //[alrt show];
  240.  
  241. }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement