views:

88

answers:

1

I'm new to this and in my application I use UISearchBar. When I enter any word to search in it the application crashes.

In my project I am using sqlite and values are fetched from the db in AppDelegate and saved in an NSMutableArray named docArray.

RootViewController.m

- (void)viewDidLoad{
    listOfItems = [[NSMutableArray alloc] init];
    NSDictionary *value = [NSDictionary dictionaryWithObject:appDelegate.docArray forKey:@"doctors"];
    [listOfItems addObject:value];
    //  Intialize copy array that will store the result of search result
    copyListOfItems = [[NSMutableArray alloc] init];
        //  Add Search bar to main view .....
    self.tableView.tableHeaderView = searchBar;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    searching = NO;
    letUserSelectRow = YES;
}

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

{
     if (searching)
    return [copyListOfItems count];
 else
    return [appDelegate.arryData count];
}


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

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

 if (searching)
 {
    waitsup *wu = [copyListOfItems objectAtIndex:indexPath.row];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]];
    cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"];
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 }
     else
 {
    waitsup *wu = [appDelegate.arryData objectAtIndex:indexPath.row];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]];
    cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"];
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     }

     return cell;
 }

-(void)searchBarTextDidBeginEditing :(UISearchBar *)theSearchBar {
NSLog(@"Now in searchBarTextDidBeginEditing");

searching = YES;
letUserSelectRow = NO;

self.tableView.scrollEnabled = NO;

//  Add Done button ........
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                           target:self action:@selector(doneSearching_clicked:)]
                                          autorelease];

}

-(NSIndexPath *)tableView :(UITableView )theTableView willSelectRowAtIndexPath :(NSIndexPath)indexPath { NSLog(@"will select Row At Index Path");

if (letUserSelectRow)
{
    return indexPath;
}
else
{
    return nil;
}

}

-(void)searchBar :(UISearchBar*)theSearchBar textDidChange :(NSString*)searchText { NSLog(@"textDidChange");

//  it is done so that data can be selected as new search
[copyListOfItems removeAllObjects];

if ([searchText length] > 0)
{
    searching = YES;
    letUserSelectRow = YES;
    self.tableView.scrollEnabled = YES;

    [self searchTableView];
}
else
{
    searching = NO;
    letUserSelectRow = NO;
    self.tableView.scrollEnabled = NO;
}

[self.tableView reloadData];

}

-(void)searchBarSearchButtonClicked :(UISearchBar*)theSearchBar { NSLog(@"searchBarSearchButtonClicked"); [self searchTableView]; }

-(void)searchTableView { NSLog(@"searchTableView");

NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (NSDictionary *dictionary in listOfItems)
{
    NSArray *array = [dictionary objectForKey:@"first_name"];
    [searchArray addObjectsFromArray:array];
}


for (waitsup *sTemp in searchArray)
{
    NSRange titleResultsRange = [[NSString stringWithFormat:@"%@ %@",sTemp.first_name, sTemp.last_name] rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if (titleResultsRange.length > 0)
    {
        NSLog(@"%@", sTemp);
        [copyListOfItems addObject:sTemp];
    }

}


[searchArray release];
searchArray = nil;

}

-(void)doneSearching_clicked:(id)sender { NSLog(@"doneSearching_clicked");

searchBar.text = @" ";
[searchBar resignFirstResponder];

letUserSelectRow = YES;
searching = NO;

self.navigationItem.rightBarButtonItem = nil;
self.tableView.scrollEnabled = YES;

[self.tableView reloadData];    

}

console show following error :

2010-07-22 13:36:16.002 wu2[1077:207] Processing Element: array

2010-07-22 13:36:16.004 wu2[1077:207] Processing Element: XML_Serializer_Tag

2010-07-22 13:36:16.005 wu2[1077:207] Processing Element: id

2010-07-22 13:36:16.005 wu2[1077:207] Processing Value: 1

2010-07-22 13:36:16.006 wu2[1077:207] Processing Element: first_name

2010-07-22 13:36:16.007 wu2[1077:207] Processing Value: Prateek

2010-07-22 13:36:16.007 wu2[1077:207] Processing Element: middle_name

2010-07-22 13:36:16.008 wu2[1077:207] Processing Value: K

2010-07-22 13:36:16.008 wu2[1077:207] Processing Element: last_name

2010-07-22 13:36:16.009 wu2[1077:207] Processing Value: Bhanot

2010-07-22 13:36:16.009 wu2[1077:207] Processing Element: title

2010-07-22 13:36:16.010 wu2[1077:207] Processing Value: Er.

2010-07-22 13:36:16.011 wu2[1077:207] Processing Element: org_name

2010-07-22 13:36:16.011 wu2[1077:207] Processing Value: Cyber

2010-07-22 13:36:16.012 wu2[1077:207] Processing Element: upin

2010-07-22 13:36:16.014 wu2[1077:207] Processing Value: 34242

2010-07-22 13:36:16.014 wu2[1077:207] Processing Element: npi

2010-07-22 13:36:16.015 wu2[1077:207] Processing Value: 2343242

2010-07-22 13:36:16.015 wu2[1077:207] Processing Element: address1

2010-07-22 13:36:16.015 wu2[1077:207] Processing Value: Maler Kotla

2010-07-22 13:36:16.016 wu2[1077:207] Processing Element: address2

2010-07-22 13:36:16.016 wu2[1077:207] Processing Value: Mohali

2010-07-22 13:36:16.018 wu2[1077:207] Processing Element: city

2010-07-22 13:36:16.018 wu2[1077:207] Processing Value: Chandigarh

2010-07-22 13:36:16.018 wu2[1077:207] Processing Element: state

2010-07-22 13:36:16.019 wu2[1077:207] Processing Value: Punja

2010-07-22 13:36:16.020 wu2[1077:207] Processing Element: zipcode

2010-07-22 13:36:16.020 wu2[1077:207] Processing Value: 12345

2010-07-22 13:36:16.021 wu2[1077:207] Processing Element: country

2010-07-22 13:36:16.022 wu2[1077:207] Processing Value: India

2010-07-22 13:36:16.022 wu2[1077:207] Processing Element: county

2010-07-22 13:36:16.023 wu2[1077:207] Processing Element: office_phone1

2010-07-22 13:36:16.025 wu2[1077:207] Processing Value: 13123131

2010-07-22 13:36:16.027 wu2[1077:207] Processing Element: fax1

2010-07-22 13:36:16.028 wu2[1077:207] Processing Value: 2131231

2010-07-22 13:36:16.028 wu2[1077:207] Processing Element: gender

2010-07-22 13:36:16.029 wu2[1077:207] Processing Value: male

2010-07-22 13:36:16.030 wu2[1077:207] Processing Element: status

2010-07-22 13:36:16.030 wu2[1077:207] Processing Value: active

2010-07-22 13:36:16.031 wu2[1077:207] Processing Element: profiletype

2010-07-22 13:36:16.031 wu2[1077:207] Processing Value: user

2010-07-22 13:36:16.032 wu2[1077:207] Processing Element: entity

2010-07-22 13:36:16.033 wu2[1077:207] Processing Value: practice

2010-07-22 13:36:16.034 wu2[1077:207] Processing Element: XML_Serializer_Tag

2010-07-22 13:36:16.035 wu2[1077:207] Processing Element: id

2010-07-22 13:36:16.035 wu2[1077:207] Processing Value: 2

2010-07-22 13:36:16.036 wu2[1077:207] Processing Element: first_name

2010-07-22 13:36:16.036 wu2[1077:207] Processing Value: Hitu

2010-07-22 13:36:16.037 wu2[1077:207] Processing Element: middle_name

2010-07-22 13:36:16.038 wu2[1077:207] Processing Value: k

2010-07-22 13:36:16.038 wu2[1077:207] Processing Element: last_name

2010-07-22 13:36:16.039 wu2[1077:207] Processing Value: bansal

2010-07-22 13:36:16.040 wu2[1077:207] Processing Element: title

2010-07-22 13:36:16.040 wu2[1077:207] Processing Value: Doctor

2010-07-22 13:36:16.041 wu2[1077:207] Processing Element: org_name

2010-07-22 13:36:16.041 wu2[1077:207] Processing Value: Cyebertron

2010-07-22 13:36:16.042 wu2[1077:207] Processing Element: upin

2010-07-22 13:36:16.042 wu2[1077:207] Processing Value: 34242

2010-07-22 13:36:16.043 wu2[1077:207] Processing Element: npi

2010-07-22 13:36:16.043 wu2[1077:207] Processing Value: 2343242

2010-07-22 13:36:16.044 wu2[1077:207] Processing Element: address1

2010-07-22 13:36:16.045 wu2[1077:207] Processing Value: Mohali

2010-07-22 13:36:16.045 wu2[1077:207] Processing Element: address2

2010-07-22 13:36:16.046 wu2[1077:207] Processing Value: chd

2010-07-22 13:36:16.046 wu2[1077:207] Processing Element: city

2010-07-22 13:36:16.047 wu2[1077:207] Processing Value: Chandigarh

2010-07-22 13:36:16.048 wu2[1077:207] Processing Element: state

2010-07-22 13:36:16.048 wu2[1077:207] Processing Value: Punja

2010-07-22 13:36:16.049 wu2[1077:207] Processing Element: zipcode

2010-07-22 13:36:16.049 wu2[1077:207] Processing Value: 12345

2010-07-22 13:36:16.050 wu2[1077:207] Processing Element: country

2010-07-22 13:36:16.050 wu2[1077:207] Processing Value: India

2010-07-22 13:36:16.051 wu2[1077:207] Processing Element: county

2010-07-22 13:36:16.051 wu2[1077:207] Processing Value: jljljlkj

2010-07-22 13:36:16.052 wu2[1077:207] Processing Element: office_phone1

2010-07-22 13:36:16.053 wu2[1077:207] Processing Value: 12-32-3-3

2010-07-22 13:36:16.053 wu2[1077:207] Processing Element: fax1

2010-07-22 13:36:16.054 wu2[1077:207] Processing Value: 12331331

2010-07-22 13:36:16.054 wu2[1077:207] Processing Element: gender

2010-07-22 13:36:16.055 wu2[1077:207] Processing Value: male

2010-07-22 13:36:16.056 wu2[1077:207] Processing Element: status

2010-07-22 13:36:16.056 wu2[1077:207] Processing Value: active

2010-07-22 13:36:16.057 wu2[1077:207] Processing Element: profiletype

2010-07-22 13:36:16.057 wu2[1077:207] Processing Value: dr411

2010-07-22 13:36:16.058 wu2[1077:207] Processing Element: entity

2010-07-22 13:36:16.058 wu2[1077:207] Processing Value: provider

2010-07-22 13:36:16.059 wu2[1077:207] No Errors

2010-07-22 13:36:20.923 wu2[1077:207] Now in searchBarTextDidBeginEditing

2010-07-22 13:36:22.899 wu2[1077:207] textDidChange

2010-07-22 13:36:22.900 wu2[1077:207] searchTableView

2010-07-22 13:36:22.901 wu2[1077:207]

2010-07-22 13:36:22.985 wu2[1077:207] textDidChange

2010-07-22 13:36:22.986 wu2[1077:207] searchTableView

2010-07-22 13:36:22.987 wu2[1077:207]

2010-07-22 13:36:23.073 wu2[1077:207] textDidChange

2010-07-22 13:36:23.074 wu2[1077:207] searchTableView

2010-07-22 13:36:23.075 wu2[1077:207]

2010-07-22 13:36:23.249 wu2[1077:207] textDidChange

2010-07-22 13:36:23.250 wu2[1077:207] searchTableView

2010-07-22 13:36:23.250 wu2[1077:207]

2010-07-22 13:36:23.449 wu2[1077:207] textDidChange

2010-07-22 13:36:23.450 wu2[1077:207] searchTableView

2010-07-22 13:36:23.450 wu2[1077:207]

2010-07-22 13:36:23.625 wu2[1077:207] textDidChange

2010-07-22 13:36:23.626 wu2[1077:207] searchTableView

2010-07-22 13:36:23.626 wu2[1077:207]

2010-07-22 13:36:23.777 wu2[1077:207] textDidChange

2010-07-22 13:36:23.778 wu2[1077:207] searchTableView

2010-07-22 13:36:23.778 wu2[1077:207]

2010-07-22 13:36:24.377 wu2[1077:207] searchBarSearchButtonClicked

2010-07-22 13:36:24.377 wu2[1077:207] searchTableView

2010-07-22 13:36:24.378 wu2[1077:207]

2010-07-22 13:36:26.157 wu2[1077:207] doneSearching_clicked

2010-07-22 13:36:26.161 wu2[1077:207] textDidChange

2010-07-22 13:36:26.161 wu2[1077:207] searchTableView

2010-07-22 13:36:26.162 wu2[1077:207]

2010-07-22 13:36:26.162 wu2[1077:207]

2010-07-22 13:36:27.071 wu2[1077:207] Now in searchBarTextDidBeginEditing

2010-07-22 13:36:28.329 wu2[1077:207] textDidChange

2010-07-22 13:36:28.330 wu2[1077:207] searchTableView

2010-07-22 13:36:30.221 wu2[1077:207] doneSearching_clicked

2010-07-22 13:36:30.224 wu2[1077:207] textDidChange

2010-07-22 13:36:30.224 wu2[1077:207] searchTableView

2010-07-22 13:36:30.225 wu2[1077:207]

2010-07-22 13:36:30.226 wu2[1077:207]

A: 

for one thing, please condense your code so we don't have to scroll through all the newlines. Furthermore, is this the dataSource of your table view, and if so, where are:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

they have to access copyListOfItems, which may fail and be the source of your crash.

What does the console show when it crashes?

edit: thanks for providing the extra info. Your problem lies here:

for (NSString *sTemp in searchArray)

searchArray contains "waitsup" objects, I think, not NSString. You cast it to an NSString pointer, but it's your responsibility that it actually points to an NSString object. Therefore you get the error that rangeOfString is called on a waitsup, which is "unrecognized". You cannot then call rangeOfString on the waitsup object, if you didn't provide the rangeOfString method in it yourself.

Looking at the rest of your code, I think you could fix it like this:

for (waitsup *sTemp in searchArray) {
    NSRange titleResultsRange = [sTemp.name_last rangeOfString:searchText options:NSCaseInsensitiveSearch];
    if (titleResultsRange.length > 0)
        [copyListOfItems addObject:sTemp];
}

but then only name_last is searched. Expanding this to include more fields is trivial.

mvds
Thanks brother, but still i get some problem that i can enter only one alphabet after this app crash.
Prateek Bhanot
could you update the question with the code you have now, and the errors you get now?
mvds
now you can check
Prateek Bhanot