Monday, 2 September 2013

IOS error while using a search bar

IOS error while using a search bar

Hi in my app I've a TableView in which I show something and I want to
search in this TableView for a text I write in the search bar, so I
followed this tutorial. In my app I'm having a problem, when I run the app
and I try to search a letter it crash and in xCode I see this information:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<__NSCFString 0x9e45990> valueForUndefinedKey:]: this class is not key
value coding-compliant for the key name.'. I checked my Storyboard to see
if I have something wrong, but it's all ok. So I guess the problem it's in
the code, I will post here my code and I hope you can help me what's
wrong.
#import "TableWasteViewController.h"
#import "WasteXmlParser.h"
#import "WasteDetailViewController.h"
@interface TableWasteViewController ()
@property(nonatomic,strong)NSArray *arrayWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWaste;
@property(nonatomic,strong)NSMutableArray *typeOfBin;
@property(nonatomic,strong)NSMutableArray *indexWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWasteBackup;
@end
@implementation TableWasteViewController
@synthesize searchBar;
@synthesize filteredWasteArray;
@synthesize typeOfWaste;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
WasteXmlParser *parser = [[WasteXmlParser alloc]init];
[parser parseWasteXml];
self.arrayWastes = [[NSArray alloc]init];
self.arrayWastes = [parser.arrayWastes mutableCopy];
self.indexWastes = [[NSMutableArray alloc]init];
typeOfWaste = [[NSMutableArray alloc]init];
self.typeOfBin = [[NSMutableArray alloc]init];
for (int i = 0; i < [self.arrayWastes count]; i++) {
[typeOfWaste addObject:[self.arrayWastes[i] objectForKey:@"type"]];
}
self.filteredWasteArray = [NSMutableArray
arrayWithCapacity:[self.typeOfWaste count]];
self.typeOfWasteBackup = [self.typeOfWaste mutableCopy];
}
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope {
[self.filteredWasteArray removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name
contains[c] %@", searchText];
filteredWasteArray = [NSMutableArray arrayWithArray:[typeOfWaste
filteredArrayUsingPredicate:predicate]];
}
#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self
filterContentForSearchText:self.searchDisplayController.searchBar.text
scope:
[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:searchOption]];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
//return self.arrayWastes.count;
if (self.tableWaste ==
self.searchDisplayController.searchResultsTableView) {
return [filteredWasteArray count];
} else {
return [self.arrayWastes count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *myFont = [UIFont fontWithName:@"Arial" size:14.0];
if (cell == nil) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = myFont;
cell.textLabel.numberOfLines = 2;
//cell.textLabel.text =
[self.arrayWastes[indexPath.row]objectForKey:@"type"];
if (self.tableWaste ==
self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text =
[self.filteredWasteArray[indexPath.row]objectForKey:@"type"];
} else {
cell.textLabel.text =
[self.arrayWastes[indexPath.row]objectForKey:@"type"];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableWaste indexPathForSelectedRow];
WasteDetailViewController *vc = segue.destinationViewController;
vc.typeOfWaste = [self.arrayWastes[indexPath.row]objectForKey:@"type"];
vc.typeOfBin = [self.arrayWastes[indexPath.row]objectForKey:@"place"];
vc.urlPic = [self.arrayWastes[indexPath.row]objectForKey:@"imgUrl"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)backToHome:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
Thank you!

No comments:

Post a Comment