我已使用下面列出的代码在uitableview中实现了搜索栏(和显示控制器)。当我在搜索文本框中输入时,我收到以下sigabrt错误:
由于未捕获的异常终止应用' 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;
}
答案 0 :(得分:0)
试试这个
如果您在按钮上创建了任何不再存在的插座或操作,则会发生这种情况。 检查所有操作和插座。 转到您的故事板并右键单击viewcontroller的黄色标志,并删除带有黄色标记的所有操作和插座。
希望它有所帮助。
libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
尝试这个答案希望它有所帮助。
答案 1 :(得分:0)
如果您无法调试当前代码,您可以在UITextField的帮助下实现搜索栏功能。只需在UITableView上方放置一个UITextField而不是搜索栏,然后设置委托。确保使用数据对象类的正确变量名称。我在这里编写代码,请告诉我这是否有帮助
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (isFromSearchResult) // BOOL to determine is from search result or not
{
return [searchResults count];
}
else
{
return myAppDelegate.offersArr.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customCell = (offersCustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(isFromSearchResult)
{
dataObj = [searchResults objectAtIndex:indexPath.row]; //dataObj is object of NSObject data class
} else
{
dataObj = [myAppDelegate.offersArr objectAtIndex:indexPath.row];
}
customCell.LblOffer.text = [dataObj.OfferTitle uppercaseString];
NSString *validity = @"VALID TILL: "; // dataObj.ValidTime;
customCell.LblValid.text = [validity stringByAppendingString:dataObj.ValidTime];
return customCell;
}
- (void)filterContentForSearchText:(NSString *)searchText范围:(NSString *)scope { //这里搜索三个字段 - 标题,内容和地址
NSPredicate *predicateOfferTitle = [NSPredicate predicateWithFormat:@"OfferTitle contains[cd] %@", searchText];
NSPredicate *predicateOfferContent = [NSPredicate predicateWithFormat:@"OfferContent contains[cd] %@", searchText];
NSPredicate *predicateAddress = [NSPredicate predicateWithFormat:@"Address contains[cd] %@", searchText];
NSArray *subPredicates = [NSArray arrayWithObjects:predicateOfferTitle, predicateOfferContent,predicateAddress, nil];
NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
searchResults = [myAppDelegate.offersArr filteredArrayUsingPredicate:orPredicate];
//NSLog(@"%@", searchResults);
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
//NSLog(@"value: %@", value);
if (![value isEqualToString:@""]) {
[self filterContentForSearchText:value scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
isFromSearchResult=YES;
}
else
{
isFromSearchResult=NO;
}
[offersTable reloadData];
return true;
}