0

テーブルのヘッダー ビュー内に検索バーがあるテーブルビューを作成しようとしています。searchDisplayController を使用してすべてを管理したいと思います。私はプログラムですべてを作成しました (IB に慣れていません) すべての正しいプロパティを設定しようとしていますが、テーブルが表示されたときにテキストを編集できないため、何かが欠けているようです。検索バーをクリックしてアニメーションを表示します。コードの一部を次に示します。

- (void)viewDidLoad {
    [super viewDidLoad];
    UISearchBar *searchBarTMP=[[UISearchBar alloc]init];
    self.searchBar=searchBarTMP;
    [searchBarTMP release];
    self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
    self.searchBar.delegate=self;
    self.searchBar.showsScopeBar=YES;
    self.searchBar.keyboardType=UIKeyboardTypeDefault;
    self.searchBar.userInteractionEnabled=YES;
    self.searchBar.multipleTouchEnabled=YES;

    self.searchBar.scopeButtonTitles=[NSArray arrayWithObjects:NSLocalizedString(@"City",@"Scope City"),NSLocalizedString(@"Postal Code",@"Scope PostalCode"),nil];
    self.tableView.tableHeaderView=searchBar;
    self.searchBar.selectedScopeButtonIndex=0;
    self.navigationItem.title=NSLocalizedString(@"Store",@"Table title");

    //SearchDisplayController creation
    UISearchDisplayController *searchDisplayControllerTMP = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.searchDisplayController=searchDisplayControllerTMP;
    [searchDisplayControllerTMP release];
    self.searchDisplayController.delegate=self;
    self.searchDisplayController.searchResultsDelegate=self;
    self.searchDisplayController.searchResultsDataSource=self;  

    //....continue
}

検索バーを単独で使用する場合は、デリゲート プロトコルを処理する必要があることはわかっていますが、Apple のサンプル コードに見られるように、searchDisplayController が管理していると思います。(IBで構築)。

なにか提案を?ありがとう、アンドレア

4

2 に答える 2

0

見つかった...テーブルビューのヘッダーを入れた後、書く必要があります

[self.searchBar sizeToFit];
于 2011-01-07T21:33:15.177 に答える
0

ARC を使用している場合は、必ずヘッダー ファイルに UISearchDisplayController の iVar を作成してください。

以下を使用して UISearchDisplayController を作成する場合:

UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];

ARCによってリリースされ、デリゲートメソッドは呼び出されず、self.searchDisplayController(UIViewControllerのプロパティ)を呼び出すと、 nil.

したがって、修正は次のとおりです。ヘッダー(.h)ファイルで:

@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
        UISearchDisplayController* searchDisplayController;
        UISearchBar *searchField;
        UITableView* tableView;
        NSArray* searchResults;
}

および実装 (.m) ファイルで:

searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
searchField.delegate = self;

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;

tableView.tableHeaderView = searchField;
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height);

そのように実装すると、コードの残りの部分でself.searchDisplayControllerとの両方を呼び出すことができます。searchDisplayController

于 2013-06-09T14:49:49.960 に答える