2013-11-05 46 views
0

大家好添加一個搜索欄,以表視圖運行和故障

我使用的代碼在下面,而無需使用故事板和它的運行還算可以,但它的崩潰當我寫搜索單詞的第二個字母。

有人能幫助我

在ViewController.h在ViewController.m

#import <UIKit/UIKit.h> 

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

@end 

#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 

其崩潰,並顯示了我這個

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

(LLDB) 線程1:EXC_BAD_ACCESS(代碼= 2,地址= 0x8中)

回答

0

用下面的代碼片斷:

#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]; 
} 
+0

就是從的Wissam的方法不同? – manujmv

+0

這裏我們保留了這個數組。 – Meenakshi