如何在ipad中从tableView中搜索数据并在同一个表中显示该数据。

时间:2013-03-19 05:08:32

标签: ios iphone objective-c uitableview full-text-search

我有文本字段,我希望如果用户输入任何数据并且它匹配tableView中的任何单元格,那么它必须显示。我使用文本字段来搜索表格中的数据。

这是怎么回事,我在tableView中填充数据如下:

     - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  {

    NSLog(@"Number of Sections");
    if(section == 0)
    return @"Sales";
    if(section == 1)
    return @"Soft Skills";
   }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


       if (section==0)
    {
    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];

    int count=[resultArray count];

    NSLog(@"resultArry Row Counts is %d",count);

    return [resultArray count];
    }

       else{
    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];

    int count=[resultArrayOne count];

    NSLog(@"resultArry Row Counts is %d",count);

    return [resultArrayOne count];
      }
         }


        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     

      {

     NSLog(@"Table Cell Data");
     static NSString *CellIdentifier = @"Cell";

         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

       }

   cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


   if (indexPath.section==0) {


    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];


    ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row];
    NSString *cellValue =theCellData.sub_Category;
    NSLog(@"Cell Values %@",cellValue);
    cell.textLabel.text = cellValue;

    return cell;

       }

      else {

    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
    ObjectData *theCellData = [resultArrayOne objectAtIndex:indexPath.row];
    NSString *cellValue =theCellData.sub_Category;
    NSLog(@"Cell Values %@",cellValue);
    cell.textLabel.text = cellValue;
    return cell;

    }
          }


      -(void)textFieldTextDidChange:(UITextField*)tf{



   NSString*test  = searchTextField.text ;

   }

4 个答案:

答案 0 :(得分:0)

尝试这样可能对你有帮助,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",  txtSearch.text];
    NSArray *ResultArray = [yourArray filteredArrayUsingPredicate:predicate];
    [table reloadData];
    return YES;
}

在上面的代码中替换你的数组。

如果要显示具有文本字段子字符串的表数据,请使用下面的代码。

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",  txtSearch.text];

答案 1 :(得分:0)

UITableview搜索数据,您可以使用SearchBar。您可以在许多Denmo项目或示例中看到,所有人都可以在SearchBar中搜索数据时使用Tableview。以下代码SearchBar

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    searchName = [[NSMutableArray alloc]init] ;
    for(NSString *name in yourArraySeaching)
    {
        NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if(r.location != NSNotFound)
        {
            [searchName addObject:name];
            tempSearch=1;
        }
    }    
}

答案 2 :(得分:0)

首先,您需要一个临时数组用于搜索目的,与主数组相同,一个数组维护搜索数组的ID 。< / p>

搜索:: bool searching;

的布尔值

代码::

-(void)textFieldTextDidChange:(UITextField*)tf {

    int len = [[tf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length];

    if (len < [mainArray count]) {

        [tempArray removeAllObjects];
        [tempArrayIds removeAllObjects];

        for(NSString *curString in tempArray)
        {
            NSString *substringRange = [curString substringWithRange:NSMakeRange(0, tf.length)];

            // Converting SearchChar and Firstchar of contestname into lower case

            NSString *searchChar = [tf lowercaseString];
            NSString *FirstChar = [substringRange lowercaseString];

            //NSLog(@"values is %@",substringRange);

            if ([FirstChar isEqualToString:searchChar]) {

                if ([tf isEqualToString:@""]) {

                    searching = false;
                    [tf resignFirstResponder];
                }
                else
                {
                    searching = true;
                    [tempArray addObject:curString];

                    indexVal = [tempArray indexOfObject:curString];

                    NSString *s = [NSString stringWithFormat:@"%d", indexVal];
                    [tempArrayIds addObject:s];
                }
           }
       }
       [tblView reloadData];
    }

表格方法::

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     if (searching)
        return [tempArray count];
    else
        return [mainArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(searching)
    {
        int k = [[tempArrayIds objectAtIndex:indexPath.row] intValue];
        cell.title.text = [mainArray objectAtIndex:k];
    }
    else
    {
        cell.title.text = [mainArray objectAtIndex:indexPath.row];
    }
    return cell;
}

希望它会帮助你。

感谢。

答案 3 :(得分:0)

我通常更喜欢使用免费提供的Sensible TableView框架来做到这一点。该框架将自动获取数据并为您提供所有搜索功能。