2009-07-03 24 views
25

私はtableViews.tableHeaderViewとしてUISearchBarを持つUITableViewを取得しています。ちょうど3.0の新しいMail.app、Notes.appなどのように。ユーザーが視界にドラッグするまでSearchBarを非表示にしたいヘッダーが表示されないようにUITableViewをスクロールしてください。

私の試行は、tableViewに実際にスクロールしたいようにいくつかの項目がある場合にのみ機能します。私はloadViewでこれを呼び出します:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self._tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

それでも、アップルはこのようなセーチバーを別に扱うようです。検索バーを外に出した後は、これ以上テーブルセルにバインドされていないように見えます(Mail.appではなくNotes.app内)。

しかし、おそらくAppleには新しい3.0の動作のための別個の方法があり、私はそれを見つけることができません。

+0

チェックアウトのカップル。 [https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TransitionGuide/AppearanceCustomization.html#//apple_ref/doc/uid/TP40013174-CH15-SW1](https://developer。 apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TransitionGuide/AppearanceCustomization.html#//apple_ref/doc/uid/TP40013174-CH15-SW1) –

答えて

33

たぶん、あなたはこのようにそれを試すことができます...

[self.tableView setContentOffset:CGPointMake(0,40)]; 
+0

完璧に動作します。 – OlivaresF

+1

テーブルビューを再読み込みした後に毎回実行する必要があります。 [self.tableView reloadData]; – Thiru

+9

行数が画面に収まる合計行数より少ない場合は機能しません。このシナリオの既知の回避策はありますか? – Zorayr

25

はあまりにも私のために働きました。私は以下を使用しました:

[self.tableView setContentOffset:CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height) animated:NO]; 

検索バーの高さを問い合わせます。

+1

私はアニメーション:はいのパラメータを追加しましたが、ラブリーでシンプルですので、その外観を改善してください。 – PKCLsoft

+0

パーフェクト!単にviewWillAppearで追加して、私が探していた結果を得ました。 – anasaitali

10

この1つはあなたにiPod.appとまったく同じ動作を取得します。これは私のために

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 

CGFloat searchBarHeight = CGRectGetHeight([[[self searchDisplayController] searchBar] frame]); 
if ([[self tableView] contentOffset].y < searchBarHeight) 
    [[self tableView] setContentOffset:CGPointMake(0, searchBarHeight)]; 
} 
3

動作します。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.bounces = YES; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    [self.tableView setContentOffset:CGPointMake(0, 44)]; 
} 
-2

I種類のそれをこのようにやってよう:

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // Hide the table view header by default. 
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
} 

あなたが本当にあなたのヘッダーがどのように背の高い心配する必要はありませんこの方法です。それだけで動作します!

+1

テーブルビューに行がロードされていないか、行がゼロの場合は、アプリケーションがクラッシュします。 – Zorayr

+0

ああ。コメントZorayrありがとう – Caborca87

0

私はその後、setContentOffset0に先頭に最初にスクロールしなければならなかった、そして、検索バーが表示されます:UIViewController` `のための新しいプロパティの

self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false) 
self.tableView.setContentOffset(CGPointMake(0, 0), animated: false) 
関連する問題