2012-01-23 1 views
3

私はsearchBarデータをデータベースから取得している、私はsearchBarを使用してiPadアプリケーションを作成し、私はtableDataという配列に格納しています。は、目的のcのUITableView動的の高さを設定することができません

このtabledata配列をmyTableViewというtableViewに渡した後、私はdidLoadメソッドのテーブルビューの高さを宣言しました。

IはdidLoadでmyTableViewの

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    // only show the status bar’s cancel button while in edit mode 

    sBar.autocorrectionType = UITextAutocorrectionTypeNo; 
    // flush the previous search content 
    [tableData removeAllObjects]; 
} 


- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    sBar.showsCancelButton = NO; 
    [myTableView setHidden:TRUE]; 
} 


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [myTableView setHidden:FALSE]; 
    [tableData removeAllObjects];// remove all data that belongs to previous search 
    myTableView.backgroundColor=[[UIColor alloc]initWithRed:4.0/255 green:24.0/255 blue:41.0/255 alpha:1.0]; 


    [self.view bringSubviewToFront:myTableView]; 

    if([searchText isEqualToString:@""]||searchText==nil){ 
     [myTableView setHidden:TRUE]; 
     [myTableView reloadData]; 
     return; 
    } 
    NSInteger counter = 0; 
    for(NSString *name in arr) 
    { 
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
     NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if(r.location != NSNotFound) 
     { 
      if(r.location== 0)//that is we are checking only the start of the naames. 
      { 
       [tableData addObject:name]; 
      } 
     } 
     counter++; 
     [pool release]; 
    } 
    [myTableView reloadData]; 
} 


- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    // if a valid search was entered but the user wanted to cancel, bring back the main list content 
    [tableData removeAllObjects]; 

    @try{ 
     [myTableView reloadData]; 
    } 
    @catch(NSException *e){ 
    } 
    [sBar resignFirstResponder]; 
    sBar.text = @""; 
} 


// called when Search (in our case “Done”) button pressed 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 

    [searchBar resignFirstResponder]; 
    [myTableView reloadData]; 
} 

が宣言のtableViewの高さは、テーブル内の要素の数に応じて、ここで

は、コードスニペットで動的になりたい:

myTableView.frame=CGRectMake(550, 00, 220,400); 

スクリーンショットを参照してください、最初の画像データには がありますが、tableViewはサイズを増やさず、2番目の画像には1つのデータしかありませんが高さですまだ修正されています。

screen screen

+0

あなたの質問のタイトルが間違っていると思います。私の理解から、あなたは 'UISearchBar'のものではなく、tableViewの高さを変更したいと思います - 私は間違っていますか? –

+0

YEs ............ – GAMA

答えて

6

はこれをしようと言っ

CGRect frame = myTableView.frame; 
frame.size.height = MIN(40 * [tableData count], 400); // 400 is the maximum height that the table view can have. You can change it to whatever you like 
myTableView.frame = frame; 

はそれがあなたのために働くなら、私に教えてください。

+1

それは魅力のように働いています! Thnxたくさん!!! – GAMA

0

あなたは のUITableViewのデリゲートメソッドを使用することができ、 - (CGFloat)のtableView:(のUITableView *)のtableView heightForRowAtIndexPath:(NSIndexPath *)indexPath。この内部

もしアレイを提示するレコードのNOに応じてテーブルビューの高さを変更- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText方法で動的

1

行の高さを設定することができます。あなたは、行の高さを知っているかもしれないので、これはウル問題を解決します

tableview.frame.size.height = ROW_HEIGHT*NO_OF_DATA_IN_ARRAY 

希望次の行を使用して動的にテーブルビューの高さを変更することができたよう。 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText方法で以下のようにテーブルの枠のサイズを変更:Atulkumar V.ジャイナ教として

+0

私は 'myTableView.frame.size.height = 40 * [tableData count];'を試しましたが、 'Expression is assignable'というエラーが表示されています。 ....これに代わるものは何ですか? – GAMA

+0

まず最初に、古い矩形を変数に保存してから高さを更新し、更新されたCGRectをテーブルビューに再割り当てする必要があります。 –

+0

このようにしてください。CGRect rect = tableview.frame;その後、rectの高さを変更します。次に、このtableview.frame = rect; ........のように、rectをtableviewに再割り当てして、これがurの問題を解決することを願っています。 –

関連する問題