2014-05-18 2 views
-1

J'ai des problèmes avec mon UISearchBar que j'ai implémenté. Mon application va compiler et s'exécuter, mais dès que j'appuie sur la barre de recherche pour commencer à taper un mot, elle plante. Quelqu'un peut-il me dire si vous voyez quelque chose de mal avec ma syntaxe? Il est supposé passer par les états que j'ai dans mon NSArray.UISearchBar émet Xcode 5

TableViewController.h:

#import <UIKit/UIKit.h> 

@interface TableViewController : UITableViewController <UISearchBarDelegate> 

@property (nonatomic, strong) NSMutableArray *results; 

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

@end 

TableViewController.m:

#import "TableViewController.h" 
#import "ViewController.h" 

@interface TableViewController() 

@end 

@implementation TableViewController 
{ 
NSArray *states; 
} 

- (NSMutableArray *)results 
{ 
if (!_results) 
{ 
    _results = [[NSMutableArray alloc] init]; 
} 
return _results; 
} 

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

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

states = [NSArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", @"Colorado", nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (void)searchThroughData 
{ 
self.results = nil; 

NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] [email protected]", self.searchBar.text]; 

self.results = [[states filteredArrayUsingPredicate:resultsPredicate] mutableCopy]; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
[self searchThroughData]; 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if(tableView == self.tableView) 
{ 
    return [states count]; 
} 

else 
{ 
    [self searchThroughData]; 
    return self.results.count; 
} 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (!cell) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 

if(tableView == self.tableView) 
{ 
    cell.textLabel.text = [states objectAtIndex:indexPath.row]; 
} 

else 
{ 
    cell.textLabel.text = self.results[indexPath.row]; 
} 

return cell; 
} 

Je suis assez nouveau à Objective-C, désolé si cela est une simple question que je devrais connaître la réponse à .

Merci

+1

Quelle est l'erreur complète? Avez-vous regardé la trace de la pile? – rmaddy

+2

Voir http://raywenderlich.com/10209/my-app-crashed-now-what-part-1 – rmaddy

+0

J'ai posté une erreur complète .. Il ressemble au lien que vous avez posté – javaGeek

Répondre

0

La première ligne de votre méthode - (void)searchThroughData définit le NSMutableArray à zéro. C'est peut-être le problème.