1

解析からクエリを実行していますが、検索バーを使用して検索しようとすると、「結果がありません」と返されます。オブジェクトを見つけたことがNSLogに表示されるため、オブジェクトを表示する際に何が間違っていたかを理解しようとしています。

私のviewDidLoad:

   - (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

    self.tableView.tableHeaderView = self.searchBar;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;
    self.searchResults = [NSMutableArray array];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

テーブル ビューの取得:

   -(void)filterResults:(NSString *)searchTerm {

    [self.searchResults removeAllObjects];

    PFQuery *query = [PFQuery queryWithClassName:@"New"];
    [query whereKeyExists:@"title"]; //this is based on whatever query you are trying to accomplish
    [query whereKey:@"title" containsString:searchTerm];

    NSArray *results  = [query findObjects];

    NSLog(@"%u", results.count);

    [self.searchResults addObjectsFromArray:results];
}

最後に、動作させたくないテーブルビュー セル:

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

           static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFObject * testObject = [self.searchResults objectAtIndex:indexPath.row];
    cell.textLabel.text = [testObject objectForKey:@"title"];

    return cell;
    }

それで:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

次に、Parse から UTTableview にロードします。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithClassName:@"New"];
    self = [super initWithCoder:aDecoder];
    if (self) {
        // The className to query on
        self.ClassName = @"New";

        //self.textKey = @"title";

        // The key of the PFObject to display in the label of the default cell style
        // self.keyToDisplay = @"text";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = NO;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = YES;

        self.loadingViewEnabled = NO;

        // The number of objects to show per page
        self.objectsPerPage = 50;
    }
    return self;

}
4

1 に答える 1

1

検索バーのデリゲート メソッドを利用できる別の方法を使用できます。

例-UISearchBarDelegateヘッダーファイルにインクルードし、さらに-

@property (strong, nonatomic) IBOutlet UISearchBar *searchSong; 

今あなたの.mファイルに

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    temp = [self searchSongsData:self.searchSong.text from:`YourArray`];
} 

+ (NSMutableArray *) searchSongsData:(NSString *)searchtext from:(NSMutableArray *)array
{
    NSMutableArray *arrayToBeReturned = [[NSMutableArray alloc] init];
    NSArray *temp = [[NSArray alloc] initWithArray:array];

    NSString *textToBeSearch = [searchtext lowercaseString];

    SongsMoviesData *songs;

    for(int i = 0; i<temp.count; i++)
    {
        songs = [temp objectAtIndex:i];

        NSString *string1 = [songs.song_title lowercaseString];
        NSString *string2 = [songs.moview_name lowercaseString];
        NSString *string3 = [songs.singer_name lowercaseString];


    if ([string1 rangeOfString:textToBeSearch].location != NSNotFound ||[string2 rangeOfString:textToBeSearch].location != NSNotFound || [string3 rangeOfString:textToBeSearch].location != NSNotFound)
    {
       // NSLog(@"%@", songs.moview_name);
        [arrayToBeReturned addObject:songs];
    }
}

return arrayToBeReturned;

}

このように、この配列を に保存しますsearch results

于 2013-08-27T05:15:09.993 に答える