在UITableview上键入搜索栏时崩溃

时间:2014-04-13 03:31:35

标签: ios objective-c uitableview uisearchbar

我在UITableview上有一个搜索栏。基本上它运行得很好,直到我开始在搜索窗口中输入。但后来我得到了Sigbart的信号。有人可以确定我哪里出错吗?我相信它是在倒数第二节中我构建了谓词并制作了NSArray的匹配。

@interface STSecondViewController ()


@property (nonatomic, copy) NSMutableArray *beerSearchNames;     @property (nonatomic, strong) UISearchDisplayController *searchController;

@end

@implementation STSecondViewController{
NSDictionary *beerContents;
NSArray *beerNames;
NSDictionary *wineContents;
NSArray *wineNames;

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

UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    NSURL *beerUrl = [[NSBundle mainBundle] URLForResource:@"BEER2"   withExtension:@"plist"];
beerContents = [NSDictionary dictionaryWithContentsOfURL:beerUrl];
beerNames = beerContents.allKeys;

self.beerSearchNames = [[NSMutableArray alloc]init];
searchController = [[UISearchDisplayController alloc]init];
searchController.searchResultsDataSource = self;

NSURL *wineUrl = [[NSBundle mainBundle] URLForResource:@"Wine" withExtension:@"plist"];
wineContents = [NSDictionary dictionaryWithContentsOfURL:wineUrl];
wineNames = wineContents.allKeys;



}

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


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView.tag == 1) {

    if (tableView == _beerTableVIew) {
        return [beerNames count];
    }
    if (tableView == _wineTableView) {
        return [wineNames count];
    }
    else{
        return 0;
    }

}
else{
    return [beerSearchNames count];
}



}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

if (tableView.tag == 1) {
    if (tableView == _beerTableVIew) {

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

        cell.textLabel.text = wineNames[indexPath.row];
    }}
else{
    cell.textLabel.text = beerSearchNames[indexPath.row];

}
return cell;
}

-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView{
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[beerSearchNames removeAllObjects];
NSLog(@"here");
if (searchString.length > 0) {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] &@", self.searchBar.text];
    for (NSString *key in beerNames) {
        NSArray *matches = [beerContents[key] filteredArrayUsingPredicate:predicate];
        [beerSearchNames addObjectsFromArray:matches];
    }
}
return YES;
}

- (IBAction)segmentChanged:(id)sender {
if (_drinkChoice.selectedSegmentIndex == 0) {
    [_beerTableVIew setHidden: NO];
    [_wineTableView setHidden:YES];

}
if (_drinkChoice.selectedSegmentIndex ==1) {
    [_beerTableVIew setHidden:YES];
    [_wineTableView setHidden:NO];
}

}
@end

0 个答案:

没有答案