0

ここに画像の説明を入力してください私はアップルのチュートリアルに目を通し、単語の配列の簡略化されたバージョンを作成しようとしています。

基本的に、ユーザーが文字aを入力し始めると、配列内のaで始まるすべての一致を動的に表示したいと思います。

配列1=自動車、飛行機、空気、風通しの良い、豆、椅子

入力を開始します:自動飛行機エアエアリー

au:自動

これは私のコードですが、最初のメソッドのshouldReloadTableForSearchStringでエラーが発生します

事前にタンクに入れてください

#import "otherSearchViewController.h"
#import "SearchViewController.h"
#import "AppDelegate.h"

@interface itemSearchViewController ()


@end

@implementation itemSearchViewController


@synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive;
@synthesize delegate;

/*
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark -
#pragma mark Lifecycle methods
*/

- (void)viewDidLoad
{

    //[super viewDidLoad];

    self.title = @"Search";

    NSString *path =[[NSBundle mainBundle] pathForResource:@"possibleItems" ofType:@"plist"];
    NSArray *content = [NSArray arrayWithContentsOfFile:path];
    listContent =[NSArray arrayWithArray:content];
    if([content count] == 0)

    {
        NSLog(@"nsma is empty");
    }
    NSLog(@"list contents%@", listContent);


   // NSLog(@"list content = %@", listContent);
        // create a filtered list that will contain products for the search results table.

    self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];

        // restore search settings if they were saved in didReceiveMemoryWarning.
    if (self.savedSearchTerm)
        {
        [self.searchDisplayController setActive:self.searchWasActive];
        [self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
        [self.searchDisplayController.searchBar setText:savedSearchTerm];

        self.savedSearchTerm = nil;
        }

    [self.tableView reloadData];
    self.tableView.scrollEnabled = YES;
}

- (void)viewDidUnload
{
        //causing problem so disabled
    //self.filteredListContent = nil;
}

- (void)viewDidDisappear:(BOOL)animated
{
        // save the state of the search UI so that it can be restored if the view is re-created
    self.searchWasActive = [self.searchDisplayController isActive];
    self.savedSearchTerm = [self.searchDisplayController.searchBar text];
    self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
}

- (void)dealloc
{
    [listContent release];
    [filteredListContent release];

    [super dealloc];
}


#pragma mark -
#pragma mark UITableView data source and delegate methods


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    /*
     If the requesting table view is the search display controller's table view, return the count of
     the filtered list, otherwise return the count of the main list.
     */
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {
        return [self.filteredListContent count];
        }
    else
        {
        return [self.listContent count];
        }
}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil)
        {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
        }

    /*
     If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
     */
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    /*
     If the requesting table view is the search display controller's table view, configure the next view controller using the filtered content, otherwise use the main list.
     */

}

#pragma mark -
#pragma mark Content Filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    /*
     Update the filtered array based on the search text and scope.
     */

    [self.filteredListContent removeAllObjects]; // First clear the filtered array.

    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */

    for (searchText in listContent){
        if ([searchText isEqualToString:scope])
            {
            NSComparisonResult result = [scope compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];

            if (result == NSOrderedSame)
                {
                [self.filteredListContent addObject:scope];
                }
            }
        }
}


#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods


//when you start typing you get this
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

        // Return YES to cause the search result table view to be reloaded.
    return YES;
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

        // Return YES to cause the search result table view to be reloaded.
    return YES;
}

@end
4

2 に答える 2

1

アレイはおそらく自動リリースです。クラスメソッドを使用してそれらを作成する代わりに、インスタンスメソッドを使用します。つまり、次のように置き換えます NSArray *content = [NSArray arrayWithContentsOfFile:path];NSArray *content = [[NSArray alloc] initWithContentsOfFile:path];

于 2012-12-01T18:11:44.330 に答える
0

欠陥のあるアレイであることが判明しました

于 2012-12-01T17:23:32.523 に答える