我已經使用下面列出的代碼在uitableview中實現了一個搜索欄(和顯示控制器)。當我輸入搜索文本框時,出現以下sigabrt錯誤:UitableView搜索文本Sigbart錯誤 - ios
由於未捕獲的異常'NSUnknownKeyException',原因:'[valueForUndefinedKey:]:該類不是符合密鑰名稱的密鑰值編碼「。
我已經重新審查了我一直在關注的教程和四重檢查代碼,但是我無法找到錯誤的原因,任何人都可以幫忙嗎?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchResults count];
} else {
return _restaurantsInCity.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"RestaurantCell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
RestarauntEntity *currentRestaurant = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
currentRestaurant = [[self.searchResults objectAtIndex:indexPath.row]];
} else {
currentRestaurant = [self.restaurantsInCity objectAtIndex:indexPath.row];
}
NSString *decodedText1 = [currentRestaurant.title stringByReplacingOccurrencesOfString:@"’" withString:@"'"];
NSString *decodedText = [decodedText1 stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
cell.textLabel.text = decodedText;
cell.textLabel.font = [UIFont fontWithName:@"avenir" size:16.0f];
cell.textLabel.textColor=[UIColor blackColor];
cell.detailTextLabel.text = currentRestaurant.city;
cell.detailTextLabel.font = [UIFont fontWithName:@"avenir" size:12.0f];
cell.detailTextLabel.textColor=[UIColor darkGrayColor];
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText];
searchResults = [_restaurantsInCity filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
嘗試創建一個異常斷點,並啓用objective-c殭屍對象來更好地瞭解崩潰。 –