尝试填充tableview时数组为空

时间:2014-07-10 10:03:08

标签: ios objective-c uitableview

我正在尝试创建第二个部分section.indexPath = 0。此部分是从两个位置之间的距离生成的对象,这些对象在didUpdateToLocation中完成。当尝试使用数组中的对象填充tableview时,它显示:

index 0 beyond bounds for empty array'

如何在tableview中显示nearStoresArray而不会出现空数组错误。

这是cellForRowAtIndexPath。这里给出了一个错误,并说nearStoresArray是空的。我猜那是因为tableview显示在didUpdateToLocation之前。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    if (indexPath.section == 0) {

        indexNo = [nearStoresArray objectAtIndex:indexPath.row];
        NSLog(@"%d", [nearStoresArray count]);

    } else if (indexPath.section == 1) {

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            indexNo = [filteredArray objectAtIndex:indexPath.row];
        } else {
            indexNo = [storesArray objectAtIndex:indexPath.row];
        }

    }

        cell.textLabel.text = [indexNo valueForKey:@"name"];


    return cell;
}

这是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];
        }



    }


}

numberOfRows方法

- (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;
    }




}

2 个答案:

答案 0 :(得分:0)

我认为您需要更新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;
     }
}

答案 1 :(得分: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];
        }
    }
}