iPhone:应用程序在TableView中使用搜索栏时崩溃

时间:2010-07-15 11:57:07

标签: iphone objective-c xcode uitableview uisearchbar

我是新手,在我的应用程序中,我使用UISearchBar。当我输入任何单词进行搜索时,应用程序崩溃。

在我的项目中,我使用sqlite,并从AppDelegate中的数据库中提取值并保存在名为NSMutableArray的{​​{1}}中。

RootViewController.m

docArray

控制台显示以下错误:

    - (void)viewDidLoad{
        listOfItems = [[NSMutableArray alloc] init];
        NSDictionary *value = [NSDictionary dictionaryWithObject:appDelegate.docArray forKey:@"doctors"];
        [listOfItems addObject:value];
        //  Intialize copy array that will store the result of search result
        copyListOfItems = [[NSMutableArray alloc] init];
            //  Add Search bar to main view .....
        self.tableView.tableHeaderView = searchBar;
        searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
        searching = NO;
        letUserSelectRow = YES;
    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
         if (searching)
        return 1;
     else
        return 1;
    }


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    {
         if (searching)
        return [copyListOfItems count];
     else
        return [appDelegate.arryData count];
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
         static NSString *CellIdentifier = @"Cell";

         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) 
     {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                       reuseIdentifier:CellIdentifier] autorelease];
     }

     if (searching)
     {
        waitsup *wu = [copyListOfItems objectAtIndex:indexPath.row];

        [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]];
        cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"];
        [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     }
         else
     {
        waitsup *wu = [appDelegate.arryData objectAtIndex:indexPath.row];

        [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]];
        cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"];
        [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         }

         return cell;
     }

    -(void)searchBarTextDidBeginEditing :(UISearchBar *)theSearchBar {
    NSLog(@"Now in searchBarTextDidBeginEditing");

    searching = YES;
    letUserSelectRow = NO;

    self.tableView.scrollEnabled = NO;

    //  Add Done button ........
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                               initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                               target:self action:@selector(doneSearching_clicked:)]
                                              autorelease];

}


-(NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath :(NSIndexPath*)indexPath {
    NSLog(@"will select Row At Index Path");

    if (letUserSelectRow)
    {
        return indexPath;
    }
    else
    {
        return nil;
    }

}


-(void)searchBar :(UISearchBar*)theSearchBar textDidChange :(NSString*)searchText {
    NSLog(@"textDidChange");

    //  it is done so that data can be selected as new search
    [copyListOfItems removeAllObjects];

    if ([searchText length] > 0)
    {
        searching = YES;
        letUserSelectRow = YES;
        self.tableView.scrollEnabled = YES;

        [self searchTableView];
    }
    else
    {
        searching = NO;
        letUserSelectRow = NO;
        self.tableView.scrollEnabled = NO;
    }

    [self.tableView reloadData];

}


-(void)searchBarSearchButtonClicked :(UISearchBar*)theSearchBar {
    NSLog(@"searchBarSearchButtonClicked");
    [self searchTableView];
}


-(void)searchTableView {
    NSLog(@"searchTableView");

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (NSDictionary *dictionary in listOfItems)
    {
        NSArray *array = [dictionary objectForKey:@"first_name"];
        [searchArray addObjectsFromArray:array];
    }


    for (waitsup *sTemp in searchArray)
    {
        NSRange titleResultsRange = [[NSString stringWithFormat:@"%@ %@",sTemp.first_name, sTemp.last_name] rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
        {
            NSLog(@"%@", sTemp);
            [copyListOfItems addObject:sTemp];
        }

    }


    [searchArray release];
    searchArray = nil;
}


-(void)doneSearching_clicked:(id)sender {
    NSLog(@"doneSearching_clicked");

    searchBar.text = @" ";
    [searchBar resignFirstResponder];

    letUserSelectRow = YES;
    searching = NO;

    self.navigationItem.rightBarButtonItem = nil;
    self.tableView.scrollEnabled = YES;

    [self.tableView reloadData];    
}

1 个答案:

答案 0 :(得分:0)

首先,请压缩您的代码,以便我们不必滚动浏览所有换行符。此外,这是您的表视图的dataSource,如果是,它在哪里:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

他们必须访问copyListOfItems,这可能会失败并成为崩溃的来源。

控制台崩溃时会显示什么?

编辑:感谢您提供额外信息。你的问题在于:

for (NSString *sTemp in searchArray)

searchArray包含“waitsup”对象,我认为不是NSString。您将其转换为NSString指针,但实际上它指向NSString对象是您的责任。因此,您会收到rangeOfStringwaitsup调用的错误,该错误是“无法识别的”。如果您没有自己提供rangeOfString方法,则无法在waitsup对象上调用rangeOfString

看看你的其余代码,我认为你可以像这样解决它:

for (waitsup *sTemp in searchArray) {
    NSRange titleResultsRange = [sTemp.name_last rangeOfString:searchText options:NSCaseInsensitiveSearch];
    if (titleResultsRange.length > 0)
        [copyListOfItems addObject:sTemp];
}

但只搜索了name_last。扩展它以包含更多字段是微不足道的。