0

我有一个文本字段,我希望如果用户输入任何数据并且它与 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

4 回答 4

0

搜索数据UITableview,您可以使用SearchBar。您可以在许多 Denmo 项目或示例中看到都可以SearchBar在 Searching Data 中使用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;
        }
    }    
}
于 2013-03-19T05:29:47.147 回答
0

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

搜索的布尔值 ::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;
}

希望它会帮助你。

谢谢。

于 2013-03-19T05:30:25.430 回答
0

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

于 2013-03-19T05:33:28.293 回答
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];
于 2013-03-19T05:16:01.067 回答