我是初学者,试图制作我的第一款应用。很抱歉,如果这是一个非常愚蠢的问题
#import "TableViewController.h"
#import "TableViewCell.h"
#import <Parse/Parse.h>
@interface TableViewController () <UISearchBarDelegate, UISearchDisplayDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchControl;
@property (nonatomic, strong) NSMutableArray *searchResults;
@end
@implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_searchControl = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchControl.searchResultsDataSource = self;
_searchControl.searchResultsDelegate = self;
_searchControl.delegate = self;
self.searchResults = [NSMutableArray array];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) queryForUsers:(NSString *)searchTerm {
PFQuery * query = [PFQuery queryWithClassName:@"User"];
[query whereKeyExists:@"username"];
[query whereKeyExists:@"FirstName"];
[query whereKeyExists:@"LastName"];
[query whereKeyExists:@"CountryName"];
[query whereKey:@"username" containsString:searchTerm];
[query whereKey:@"FirstName" containsString:searchTerm];
[query whereKey:@"LastName" containsString:searchTerm];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Successful query!");
[self.searchResults addObjectsFromArray:objects];
}
else {
NSLog(@"Error in query!");
}
}];
}
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self queryForUsers:searchString];
return YES;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return 0;
}
if (tableView == self.searchControl.searchResultsTableView) {
return self.searchResults.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
else if (tableView == self.searchControl.searchResultsTableView) {
PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
cell.userLabel.text = [searchedUser objectForKey:@"username"];
cell.userNameLabel.text = [searchedUser objectForKey:@"FirstName" @"LastName"];
cell.countryLabel.text = [searchedUser objectForKey:@"CountryName"];
}
return cell;
}
这是我的代码,我觉得我正确地调用了一切,而且一切都很好。 NSLog(@&#34;成功查询。&#34;)在我搜索时显示,但不显示任何内容。所以我不知道视图出了什么问题。任何帮助都会很棒,谢谢你提前!