2

この質問について無知で申し訳ありませんが、私はまだ基​​本を理解しようとしており、これについて数日間壁に頭をぶつけています。

私は基本的に、ユーザーが検索できるようにするためにNSArray使用している を持っています。これは、ユーザーが入力するときに、検索条件に文字を含む結果をフィルタリングします。UITableView

接続するためのチュートリアルはかなりありますが、それらはすべて xib ファイルを使用してUITableViewsおり、プログラムですべてを追加しUISearchDisplayControllersたいと考えています。

ユーザーがボタンをタップするUITableViewと、その内容が aに表示される があり、同じビューの上部にを追加したように見え、. しかし、3つすべてをうまくプレイすることはできないようです。UIViewUISearchBarUISearchDisplayController

これが私がこれまでに持っているものです

- (IBAction)createSearchList:(id)sender {

//Make the list
AAPjsonParse * makeTheFullList = [[AAPjsonParse alloc] init];    
fullListOfRecipesForTableView = [makeTheFullList generateFullListOfRecipes];
fullListForSearchResultsInTableView = fullListOfRecipesForTableView;
recipeListForTable = [fullListForSearchResultsInTableView allKeys];
recipeListForTable = [recipeListForTable sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


//Create the UITableView
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 300, 724) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;

//Add the TableView to the view
[self.searchView addSubview:tableView];

//Create a UISearchBar for the TableView

UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
//Set the searchBar as the delegate
searchBar.delegate = self;
//Add the searchBar to the UITableView
[self.searchView addSubview:searchBar];

//Create the searchViewController
UISearchDisplayController * searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
//Set the dataSource for the search as self
searchController.searchResultsDataSource = self;
//Set the results delegate as self – this is the view that's overlaid onto the full listing view (I think?)
searchController.delegate = self;

}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [recipeListForTable count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @"RecipeCell";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSString * cellTitle = [recipeListForTable objectAtIndex:indexPath.row];

cell.textLabel.text = cellTitle;

return cell;
}

また、さまざまなビットを格納するために、実装ファイルの上部にいくつかのものがあります

{    
NSArray * recipeListForTable;
NSMutableArray * recipeListForSearch;
int selectedRecipeIndex;

NSDictionary * fullListOfRecipesForTableView;
NSMutableDictionary * fullListForSearchResultsInTableView;

UITableView * tableView;
}
4

1 に答える 1

1

このコードは検索バーを追加しますtableview

  searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tblView.frame), 44)];
        searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
        searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
       // searchBar.tintColor = [UIColor lightGrayColor];



    searchBar.placeholder = @"What are you eating today...";
    searchBar.delegate = self;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;

    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

       [self.tblView setTableHeaderView:searchBar];

searchbar check を追加した後、データを渡し、デリゲートデータソースを設定する方法を確認したら、このチュートリアルを実行することをお勧めします。

于 2014-05-12T13:38:44.527 に答える