IOS Coredata自动填充文本字段与搜索

时间:2015-10-21 08:02:48

标签: ios objective-c core-data uitextfield

我正在研究coredatabase项目并实现文本字段自动填充搜索功能。我是coredata的新手。所以,我无法理解如何在Autofilltableview中获取搜索文本的结果以及如何使用Nspredicate从coredata中搜索字符串。

这是我的尝试:

  - (BOOL)textField:(UITextField *)textField
    shouldChangeCharactersInRange:(NSRange)range
    replacementString:(NSString *)string
    {
        if (textField==_textField1) {
            [popupView bringSubviewToFront:autocompleteTableView];
            [autocompleteTableView setHidden:NO];

            NSString *substring = [NSString stringWithString:textField.text];
            substring = [substring stringByReplacingCharactersInRange:range withString:string];
            [self searchAutocompleteEntriesWithSubstring:substring];
    }
            return YES;
            //[popupView bringSubviewToFront:autoCompleteTextField];

            //[self searchedbuttonclcked ];



    }


        - (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

        //searcharray=[NSMutableArray new];
        AppDelegate *appdelegate=[[UIApplication sharedApplication]delegate];
        NSManagedObjectContext *contex;
        contex=[appdelegate managedObjectContext];
        NSEntityDescription *entity = [NSEntityDescription
                                       entityForName:@"Iw" inManagedObjectContext:contex];
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

        [fetchRequest setEntity:entity];
         NSError *error;
        NSArray *queryArray;
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"name contains[c] %@",self.textField1.text];

        [fetchRequest setPredicate:predicate];
        queryArray = [contex executeFetchRequest:fetchRequest error:&error];

      [autocompleteTableView reloadData]; // This is tableview which contains result below the textfield 
    }

    #pragma mark -->Autocomplete tablevie methods
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        id  sectionInfo =
        [[_fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];

    }

    - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
      //  _searchfetch  = [_fetchedResultsController objectAtIndexPath:indexPath];


        NSManagedObject *fetchsidename = [self.searchfetch objectAtIndex:indexPath.row];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [fetchsidename valueForKey:@"name"]]];

       }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        [self configureCell:cell atIndexPath:indexPath];

        return cell;
    }

Autofill tableview

0 个答案:

没有答案