2017-05-17 13 views
0

私はバックエンド用のGoogle Firebaseとフロントエンド用のObjective-Cを使用しているアプリケーションを作成しました。UITableViewでの検索バーの実装目的C - iOS 9以降

UITableViewにデータを格納して表示することに成功しました。

ここでは、を追加しようとしています。をテーブルビューの上に追加し、インターネットから多くの例を試しましたが、ほとんどが古くなっています。

テーブルビューの上に検索バーを追加するにはどうすればよいですか?iOS 9以降の目的のC

また、検索バーに入力するときのデータをフィルタリングする方法はありますか?

ありがとうヒープ

+0

ただ、テーブルビュー(ドラッグ&ドロップ)の上に検索バーを追加します。 – GeneCode

+0

簡単です!おかげで – Deepak

答えて

0

.hファイルで宣言します.Mファイルに

BOOL isFiltered; 
    NSMutableArray *arrsearchresult ,*arrsearchname; 
    NSMutableArray *arrfullname; 
@property (strong, nonatomic) IBOutlet UITableView *tblAppoinment; 

宣言:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
       { 
        // Return the number of rows in the section. 
        /*if (tableView == self.searchDisplayController.searchResultsTableView) { 
        return [arrsearchresult count]; 

        } else { 
        return [arrfullname count]; 
        }*/ 

        if(isFiltered) 
        { 
         return [arrsearchresult count]; 
        } 

        return [arrfullname count]; 
       } 


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


static NSString *simpleTableIdentifier = @"SimpleTableCell"; 

    AppoinmentTableViewCell *cell = (AppoinmentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AppoinmentTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
     if(isFiltered) 
      { 
       cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row]; 

      } 




    } 

    if(isFiltered) 
    { 
     cell.lblFullname.text = [arrsearchname objectAtIndex:indexPath.row]; 


    } 
    } 




       - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
        isFiltered = YES; 
       } 



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



          if(searchText.length==0) 
          { 
           isFiltered=NO; 
          } 

          else 
          { 
           isFiltered=YES; 
           arrsearchresult=[[NSMutableArray alloc]init]; 
           arrsearchdate=[[NSMutableArray alloc]init]; 
           arrsearchname=[[NSMutableArray alloc]init]; 
           for (NSString * str in arrfullname) 
           { 
            NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

            NSInteger index; 
            if(stringRange.location != NSNotFound) 
            { 
             [arrsearchresult addObject:str]; 
             index = [arrfullname indexOfObject:str]; 
             [arrsearchdate addObject:[arrleadCreatedDate objectAtIndex:index]]; 
             [arrsearchname addObject:str]; 
            } 
           } 
          } 



        [self.tblAppoinment reloadData]; 
       } 

       -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ 
        [self.tblAppoinment resignFirstResponder]; 
       } 

       - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
        isFiltered=NO; 
        [self.tblAppoinment reloadData]; 

       } 
+0

@Deepakこのコードを試してください... –

+0

ありがとうswapnil、私はあなたのコードを試します – Deepak

0

検索バーについて話していますが、あなたの要求に応じて、検索コントローラを検索バー単体よりも使用する方が良いです。多くのデリゲートメソッドがありますので、これらと一緒に見つけることができます。余分な努力のために頭痛を負う必要はありません。

+0

ありがとう、それは素晴らしい作品! – Deepak

+0

検索コントローラを入力すると、どのようにフィルタリングできますか? – Deepak

+0

それが役に立つなら、マークを付けても役に立つでしょう。 :) –

0

このコードはフィルタ配列用です。

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange: 
(NSRange)range replacementText:(NSString *)text 
{ 
    NSArray *resultarray; 

    NSString *strSearch = [NSString stringWithFormat:@"%@",searchB.text]; 

     if ([strSearch isEqualToString:@""]) 
     { 
     [self showAllData]; 

     return; 
     } 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@",strSearch]; 

    resultarray = [self.arrayALLData filteredArrayUsingPredicate:predicate]; 

    [self.arrayPeopleList removeAllObjects]; 

    [self.arrayPeopleList addObjectsFromArray:resultarray]; 

    [_tblView reloadData]; 

    return YES; 
} 
+0

この検索は、@ [@ "jignesh"、@ "mayani"、@ "Rajesh"]のような文字列配列を持っていると動作します –

+0

ありがとうJignesh、それは私を助けました...今このコードを使用するとクラッシュすることはありません。私はまだそれを持って遊んでいる – Deepak

+0

私はGoogleのfirebaseのようなバックエンドのサービスから配列をフェッチしようとしている...あなたはそれが文字列配列で動作すると言ったいくつかの異なるアプローチを使用する必要があると思いますか? – Deepak

関連する問題