Question

hi everyone

i am using the code in below without using storyboard and its run ok, but its crash when i write the second letter of searching word.

anyone can help me

in ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (retain, nonatomic) IBOutlet UILabel *nameLabel;

@end  

in ViewController.m

#import "ViewController.h"
#import "DetailController.h"
@interface ViewController ()

@end

@implementation ViewController  {

    NSArray *peopleName;
    NSArray *searchResult;

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    peopleName = [[NSArray alloc] initWithObjects:@"john", @"Waseem", @"Bruce", @"Ammar", @"Londol", nil];
}

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

- (void)dealloc {
    [_nameLabel release];
    [super dealloc];
}

//

#pragma mark - Table view data source

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

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

    } else {
        return [peopleName count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [peopleName objectAtIndex:indexPath.row];
    }
    return cell;
}

  #pragma mark - UISearchDisplayController delegate methods

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];
    searchResult = [peopleName filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}
@end

its crash and show me this

cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];

(lldb) Thread 1:EXC_BAD_ACCESS (code=2, address=0x8)

Was it helpful?

Solution

Used the below code snippet:

#pragma mark - UISearchDisplayController delegate methods

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];
    [searchResult release];
    searchResult = [peopleName filteredArrayUsingPredicate:resultPredicate]retain];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top