Ios 尝试填充tableview时数组为空

Ios 尝试填充tableview时数组为空,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试创建第二个节,其中section.indepath=0。这一部分是从两个位置之间的距离生成的对象,这是在didUpdateToLocation中完成的。尝试使用阵列中的对象填充tableview时,会显示: index 0 beyond bounds for empty array' 如何在tableview中显示nearStoresArray而不获得空数组错误 这是cellForRowAtIndexPath。这里它给出一个错误,并表示nearStoresArray为空。我想这是因为

我正在尝试创建第二个节,其中section.indepath=0。这一部分是从两个位置之间的距离生成的对象,这是在didUpdateToLocation中完成的。尝试使用阵列中的对象填充tableview时,会显示:

index 0 beyond bounds for empty array'
如何在tableview中显示nearStoresArray而不获得空数组错误

这是cellForRowAtIndexPath。这里它给出一个错误,并表示nearStoresArray为空。我想这是因为tableview显示在didUpdateToLocation之前

下面是didUpdateToLocation方法。此方法创建nearStoresArray,效果良好

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [locationManager stopUpdatingLocation];
    locationManager = nil;

    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {

        for (int i = 0; i < [storesArray count]; i++) {

            CLLocationDegrees latitude = [[[storesArray objectAtIndex:i] valueForKey:@"lat"] doubleValue];
            CLLocationDegrees longitude = [[[storesArray objectAtIndex:i] valueForKey:@"long"] doubleValue];

            CLLocation *checkPosition = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

            CLLocationDistance distance = [checkPosition distanceFromLocation:currentLocation];


            float distanceInKm = distance / 1000;


            if (distanceInKm < 5) {

                [nearStoresArray removeAllObjects];

                [nearStoresArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
                [[storesArray objectAtIndex:i] valueForKey:@"id"], @"id",
                [[storesArray objectAtIndex:i] valueForKey:@"name"], @"name",
                [[storesArray objectAtIndex:i] valueForKey:@"lat"], @"lat",
                [[storesArray objectAtIndex:i] valueForKey:@"long"], @"long",
                [[storesArray objectAtIndex:i] valueForKey:@"address"], @"address",
                [[storesArray objectAtIndex:i] valueForKey:@"zip"], @"zip"
                , nil]];

               NSLog(@"%@", nearStoresArray);


            }


            [self.tableView reloadData];
        }



    }


}

我相信您需要更新numberOfRowsInSection方法,如下所示-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [nearStoresArray count];

    }
    else if (section == 1) {

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return [filteredArray count];
        } 
        else {
            return [storesArray count];
        }
    }
    else {
        return 0;
     }
}

您可以直接访问以下部分:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [nearStoresArray count];

    }
    else if (section == 1) {

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return [filteredArray count];
        } 
        else {
            return [storesArray count];
        }
    }
}

您在哪里定义了数组?添加代码,以及如何定义numberOfRows方法?我在viewDidLoad中定义了它:nearStoresArray=[[NSMutableArray alloc]init];numberOfRows addedWhere是indexPath定义的?所以现在它与OPs代码完全相同,只是它不处理section>1的情况。你能解释一下它实现了什么吗,因为我不清楚。早些时候他的代码根本没有处理节条件。我想他已经更新了这个。然而,如果他的问题没有解决,我将删除这个答案。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [nearStoresArray count];

    }
    else if (section == 1) {

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return [filteredArray count];
        } 
        else {
            return [storesArray count];
        }
    }
    else {
        return 0;
     }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [nearStoresArray count];

    }
    else if (section == 1) {

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return [filteredArray count];
        } 
        else {
            return [storesArray count];
        }
    }
}