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

私は通常、無料で入手できるSensibleTableViewフレームワークを使用してそれを行うことを好みます。フレームワークはデータをフェッチし、すべての検索機能を自動的に提供します。

于 2013-03-19T05:33:28.293 に答える
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;
        }
    }    
}
于 2013-03-19T05:29:47.147 に答える
0

まず、メイン配列と同じ目的で検索用にもう 1 つの一時配列が必要です。もう 1 つの配列は検索配列の 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

このようにしてみてください。

- (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 に答える