非空数组的出界错误 - 发生了什么?

时间:2014-01-29 05:26:12

标签: ios iphone uitableview uisearchdisplaycontroller

我正在尝试实现一个searchResultsTableView,它会根据用户的搜索重新加载表格。在numberOfRowsInSection函数中,我知道这是异常,我有以下内容:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return self.objects.count;
    } else {
        NSLog(@"The count of self.searchresults is %i", self.searchResults.count); //Shows as non-zero
        return self.searchResults.count;
    }

}

我感到困惑的是我在return语句之前设置了一个断点,我知道我根据NSLog获得了非0结果。在返回非0数字之后,立即抛出异常,并且我得到异常“索引0超出空数组的边界”

我做错了什么?

我也确保只手动重新加载表,所以在shouldReloadTableForSearchString中,我有以下方法:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    if (searchString.length > 3){
        [self.searchResults removeAllObjects];

        PFQuery *query = [PFUser query];
        [query whereKey:@"username" containsString:searchString];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error){
                NSLog(@"There is an error");
            } else {
                [self.searchResults addObjectsFromArray:objects];
                NSLog(@"self.resultResults count %i", self.searchResults.count);
                NSLog(@"The objects are %@", self.searchResults);
                [self.searchDisplayController.searchResultsTableView reloadData];
            }
        }];

    }
    return NO;
}

谢谢!

3 个答案:

答案 0 :(得分:1)

似乎你的应用程序没有崩溃

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

检查调用objectAtIndex:方法的位置,看看是否正在调用无效索引。

答案 1 :(得分:0)

试试这个,

searchDisplayController:更改为

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    if (searchString.length > 3){

        PFQuery *query = [PFUser query];
        [query whereKey:@"username" containsString:searchString];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error){
                NSLog(@"There is an error");
            } else {
                [self.searchResults setArray:objects];
                NSLog(@"self.resultResults count %i", self.searchResults.count);
                NSLog(@"The objects are %@", self.searchResults);
               [self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
            }
       }];

    }
    return NO;
}

答案 2 :(得分:0)

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    if (searchString.length > 3){
         if(self.searchResults){
            [self.searchResults release];
             self.searchResults=nil;
         }

       // [self.searchResults removeAllObjects];

        PFQuery *query = [PFUser query];
        [query whereKey:@"username" containsString:searchString];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error){
                NSLog(@"There is an error");
            } else {
               // [self.searchResults addObjectsFromArray:objects];
                self.searchResults=[[NSMutableArray alloc] initWithArray:objects];

                NSLog(@"self.resultResults count %i", self.searchResults.count);
                NSLog(@"The objects are %@", self.searchResults);
                [self.searchDisplayController.searchResultsTableView reloadData];
            }
        }];

    }
    return NO;
}

而不是[self.searchResults removeAllObjects];使用

if(self.searchResults){
            [self.searchResults release];
             self.searchResults=nil;
         }

并替换:

 [self.searchResults addObjectsFromArray:objects]; 

 self.searchResults=[[NSMutableArray alloc] initWithArray:objects];