Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用搜索栏时发生IOS错误_Ios_Uitableview_Uisearchbar - Fatal编程技术网

使用搜索栏时发生IOS错误

使用搜索栏时发生IOS错误,ios,uitableview,uisearchbar,Ios,Uitableview,Uisearchbar,嗨,在我的应用程序中,我有一个TableView,在其中我显示了一些东西,我想在这个TableView中搜索我在搜索栏中写的文本,所以我遵循了这个。 在我的应用程序中,我遇到了一个问题,当我运行应用程序并尝试搜索一个字母时,它会崩溃,在xCode中我看到以下信息:由于未捕获的异常“NSUnknownKeyException”终止应用程序,原因:“[valueForUndefinedKey::]:此类不符合密钥名称的键值编码要求。”。我检查了我的故事板,看看我是否有什么错误,但一切都好。所以我猜问

嗨,在我的应用程序中,我有一个TableView,在其中我显示了一些东西,我想在这个TableView中搜索我在搜索栏中写的文本,所以我遵循了这个。 在我的应用程序中,我遇到了一个问题,当我运行应用程序并尝试搜索一个字母时,它会崩溃,在xCode中我看到以下信息:
由于未捕获的异常“NSUnknownKeyException”终止应用程序,原因:“[valueForUndefinedKey::]:此类不符合密钥名称的键值编码要求。”
。我检查了我的故事板,看看我是否有什么错误,但一切都好。所以我猜问题出在代码中,我会在这里发布我的代码,希望你能帮我找出问题所在

#import "TableWasteViewController.h"
#import "WasteXmlParser.h"
#import "WasteDetailViewController.h"

@interface TableWasteViewController ()

@property(nonatomic,strong)NSArray *arrayWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWaste;
@property(nonatomic,strong)NSMutableArray *typeOfBin;
@property(nonatomic,strong)NSMutableArray *indexWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWasteBackup;

@end

@implementation TableWasteViewController
@synthesize searchBar;
@synthesize filteredWasteArray;
@synthesize typeOfWaste;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    WasteXmlParser *parser = [[WasteXmlParser alloc]init];
    [parser parseWasteXml];
    self.arrayWastes = [[NSArray alloc]init];
    self.arrayWastes = [parser.arrayWastes mutableCopy];
    self.indexWastes = [[NSMutableArray alloc]init];
    typeOfWaste = [[NSMutableArray alloc]init];
    self.typeOfBin = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self.arrayWastes count]; i++) {
        [typeOfWaste addObject:[self.arrayWastes[i] objectForKey:@"type"]];
    }

    self.filteredWasteArray = [NSMutableArray arrayWithCapacity:[self.typeOfWaste count]];

    self.typeOfWasteBackup = [self.typeOfWaste mutableCopy];
}

#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    [self.filteredWasteArray removeAllObjects];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];
    filteredWasteArray = [NSMutableArray arrayWithArray:[typeOfWaste filteredArrayUsingPredicate:predicate]];
}

#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return self.arrayWastes.count;
    if (self.tableWaste == self.searchDisplayController.searchResultsTableView) {
        return [filteredWasteArray count];
    } else {
        return [self.arrayWastes count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIFont *myFont = [UIFont fontWithName:@"Arial" size:14.0];
    if (cell == nil) {
         cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.font = myFont;
    cell.textLabel.numberOfLines = 2;
    //cell.textLabel.text = [self.arrayWastes[indexPath.row]objectForKey:@"type"];
    if (self.tableWaste == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.filteredWasteArray[indexPath.row]objectForKey:@"type"];
    } else {
        cell.textLabel.text = [self.arrayWastes[indexPath.row]objectForKey:@"type"];
    }
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSIndexPath *indexPath = [self.tableWaste indexPathForSelectedRow];
    WasteDetailViewController *vc = segue.destinationViewController;
    vc.typeOfWaste = [self.arrayWastes[indexPath.row]objectForKey:@"type"];
    vc.typeOfBin = [self.arrayWastes[indexPath.row]objectForKey:@"place"];
    vc.urlPic = [self.arrayWastes[indexPath.row]objectForKey:@"imgUrl"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)backToHome:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
#导入“TableWasteViewController.h”
#导入“WasteXmlParser.h”
#导入“WasteDetailViewController.h”
@接口TableViewController()
@属性(非原子、强)NSArray*阵列废物;
@属性(非原子,强)NSMutableArray*类型浪费;
@属性(非原子,强)NSMutableArray*typeOfBin;
@属性(非原子,强)NSMutableArray*indexWastes;
@属性(非原子,强)NSMUTABLEARRY*typeOfWasteBackup;
@结束
@视图控制器的实现
@综合搜索栏;
@合成滤波器阵列;
@综合废物类型;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
WasteXmlParser*parser=[[WasteXmlParser alloc]init];
[解析器解析XML];
self.arrayWastes=[[NSArray alloc]init];
self.arrayWastes=[parser.arrayWastes mutableCopy];
self.indexWastes=[[NSMutableArray alloc]init];
typeOfWaste=[[NSMutableArray alloc]init];
self.typeOfBin=[[NSMutableArray alloc]init];
for(int i=0;i<[self.arrayWastes count];i++){
[typeOfWaste addObject:[self.arrayWastes[i]objectForKey:@“type”];
}
self.filteredWasteArray=[NSMutableArray阵列容量:[self.typeOfWaste count]];
self.typeOfWasteBackup=[self.typeofwastemutablecopy];
}
#pragma标记内容过滤
-(void)filterContentForSearchText:(NSString*)搜索文本范围:(NSString*)范围{
[self.filteredWasteArray removeAllObjects];
NSPredicate*谓词=[NSPredicate谓词格式:@“SELF.name包含[c]@”,searchText];
filteredWasteArray=[NSMutableArray arrayWithArray:[typeOfWaste filteredArrayUsingPredicate:predicate];
}
#pragma标记-UISearchDisplayController委托方法
-(BOOL)searchDisplayController:(UISearchDisplayController*)控制器应重新加载搜索字符串的表:(NSString*)搜索字符串{
[self-filterContentForSearchText:searchString作用域:
[[self.searchDisplayController.searchBar ScopeButtonTiles]对象索引:[self.searchDisplayController.searchBar selectedScopeButtonIndex]];
返回YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)控制器应重新加载搜索范围的表:(NSInteger)搜索选项{
[self-filterContentForSearchText:self.searchDisplayController.searchBar.text范围:
[[self.searchDisplayController.searchBar ScopeButtonTiles]对象索引:搜索选项]];
返回YES;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回self.arrayWastes.count;
if(self.tableWaste==self.searchDisplayController.searchResultsTableView){
返回[filteredWasteArray计数];
}否则{
返回[self.arrayWastes count];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont*myFont=[UIFont fontWithName:@“Arial”大小:14.0];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textLabel.font=myFont;
cell.textlab.numberOfLines=2;
//cell.textlab.text=[self.arrayWastes[indexPath.row]objectForKey:@“type”];
if(self.tableWaste==self.searchDisplayController.searchResultsTableView){
cell.textlab.text=[self.filteredWasteArray[indexPath.row]objectForKey:@“type”];
}否则{
cell.textlab.text=[self.arrayWastes[indexPath.row]objectForKey:@“type”];
}
返回单元;
}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方{
NSIndexPath*indexPath=[self.tableWaste indexPathForSelectedRow];
WasteDetailViewController*vc=segue.destinationViewController;
vc.typeOfWaste=[self.arrayWastes[indexPath.row]objectForKey:@“type”];
vc.typeOfBin=[self.arrayWastes[indexPath.row]objectForKey:@“place”];
vc.urlPic=[self.arrayWastes[indexPath.row]objectForKey:@“imgUrl”];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
}
-(iAction)返回主页:(id)发件人{
[自我解除视图控制器激活:是完成:无];
}
@结束
谢谢大家!

更新 我在这里发布了我的故事板屏幕,您可以在其中看到tableview控制器的连接


再次检查你的故事板,在那里你会发现孤儿
IBOutlet
名为“name”,删除它并构建。它应该很好用。祝你好运

再次检查您的故事板,在那里您应该找到名为“name”的孤立的
IBOutlet
,删除它并构建。它应该很好用。祝你好运

如果您在xib中提供了正确的课程,请查看一次。查看您提供的插座。删除它,然后重新正确设置并检查它。当我将搜索栏连接到搜索显示控制器时,我可以看到错误。如果您在xib中提供了正确的类,请检查一次。查看您提供的插座。雷莫夫