0

请注意新编辑!!!!

所以我正在尝试创建一个从数组加载注释并将它们显示在地图上的应用程序......然后当您在左侧的 UITable 中选择一个位置时,它会缩放到右侧地图上的位置。下面是加载注解的代码,设置section的数量(在UITable中查看),设置每个section的行数,最后将注解名称写入UITable,以便它可以用作选择器。

问题:应用程序运行,但左侧的 UITable(用于选择位置)在 Hotels 部分中不显示任何内容,在 AerialView 部分中显示单词“Empty”,并在 Streets 部分中显示一个位置,其余行在街道部分空白。

所以很明显注释名称没有正确加载到我的 UITable 中。有任何想法吗?

-(void) loadOutAnnotations
{

    CLLocationCoordinate2D workingCoordinate;

    //***********HOTELS**************************

    workingCoordinate.latitude = 40.763856;
    workingCoordinate.longitude = -73.973034;
    iCodeBlogAnnotation *hotel1 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Hotel 1"];
    [hotel1 setSubtitle:@"This is Hotel #1"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel1];

    workingCoordinate.latitude = 50.763856;
    workingCoordinate.longitude = -83.973034;
    iCodeBlogAnnotation *hotel2 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Hotel 2"];
    [hotel1 setSubtitle:@"This is Hotel #2"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel2];

    workingCoordinate.latitude = 60.763856;
    workingCoordinate.longitude = -93.973034;
    iCodeBlogAnnotation *hotel3 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Hotel 3"];
    [hotel1 setSubtitle:@"This is Hotel #3"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel3];

    workingCoordinate.latitude = 10.763856;
    workingCoordinate.longitude = -23.973034;
    iCodeBlogAnnotation *hotel4 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Hotel 4"];
    [hotel1 setSubtitle:@"This is Hotel #4"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel4];

    workingCoordinate.latitude = 30.763856;
    workingCoordinate.longitude = -43.973034;
    iCodeBlogAnnotation *hotel5 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Hotel 5"];
    [hotel1 setSubtitle:@"This is Hotel #5"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeHotels];

    [mapView addAnnotation:hotel5];

    // *****************AERIALS*****************************

    workingCoordinate.latitude = 25.763856;
    workingCoordinate.longitude = -15.973034;
    iCodeBlogAnnotation *aerial1 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Aerial 1"];
    [hotel1 setSubtitle:@"This is Aerial #1"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial1];


    workingCoordinate.latitude = 35.763856;
    workingCoordinate.longitude = -25.973034;
    iCodeBlogAnnotation *aerial2 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Aerial 2"];
    [hotel1 setSubtitle:@"This is Aerial #2"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial2];

    workingCoordinate.latitude = 45.763856;
    workingCoordinate.longitude = -35.973034;
    iCodeBlogAnnotation *aerial3 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Aerial 3"];    
    [hotel1 setSubtitle:@"This is Aerial #3"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial3];

    workingCoordinate.latitude = 55.763856;
    workingCoordinate.longitude = -45.973034;
    iCodeBlogAnnotation *aerial4 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Aerial 4"];
    [hotel1 setSubtitle:@"This is Aerial #4"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial4];

    workingCoordinate.latitude = 65.763856;
    workingCoordinate.longitude = -55.973034;
    iCodeBlogAnnotation *aerial5 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Aerial 5"];
    [hotel1 setSubtitle:@"This is Aerial #5"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeAerialShots];

    [mapView addAnnotation:aerial5];


     // *****************STREETS*****************************

    workingCoordinate.latitude = 27.763856;
    workingCoordinate.longitude = -37.973034;
    iCodeBlogAnnotation *street1 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Street 1"];
    [hotel1 setSubtitle:@"This is Street #1"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street1];

    workingCoordinate.latitude = 37.763856;
    workingCoordinate.longitude = -47.973034;
    iCodeBlogAnnotation *street2 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Street 2"];
    [hotel1 setSubtitle:@"This is Street #2"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street2];

    workingCoordinate.latitude = 57.763856;
    workingCoordinate.longitude = -67.973034;
    iCodeBlogAnnotation *street3 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Street 3"];
    [hotel1 setSubtitle:@"This is Street #3"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street3];

    workingCoordinate.latitude = 77.763856;
    workingCoordinate.longitude = -87.973034;
    iCodeBlogAnnotation *street4 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Street 4"];
    [hotel1 setSubtitle:@"This is Street #4"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street4];

    workingCoordinate.latitude = 87.763856;
    workingCoordinate.longitude = -97.973034;
    iCodeBlogAnnotation *street5 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
    [hotel1 setTitle:@"Street 5"];
    [hotel1 setSubtitle:@"This is Street #5"];
    [hotel1 setAnnotationType:iCodeBlogAnnotationTypeStreets];

    [mapView addAnnotation:street5];
}


- (iCodeBlogAnnotationView *)mapView:(MKMapView *)mapView 
                   viewForAnnotation:(id<MKAnnotation>)annotation
{
    iCodeBlogAnnotationView *annotationView = nil;
    iCodeBlogAnnotation *myAnnotation = (iCodeBlogAnnotation *)annotation;

    if(myAnnotation.annotationType == iCodeBlogAnnotationTypeHotels)
    {
        NSString *identifier = @"Hotels";
        iCodeBlogAnnotationView *newAnnotationView = (iCodeBlogAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (newAnnotationView == nil)
        {
            newAnnotationView = [[[iCodeBlogAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier]autorelease];  
        }

        annotationView = newAnnotationView;
    }
    else if(myAnnotation.annotationType == iCodeBlogAnnotationTypeAerialShots)
    {
        NSString *identifier = @"AerialShots";
        iCodeBlogAnnotationView *newAnnotationView = (iCodeBlogAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (newAnnotationView == nil)
        {
            newAnnotationView = [[[iCodeBlogAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier]autorelease];
        }

        annotationView = newAnnotationView;
    }
    else if(myAnnotation.annotationType == iCodeBlogAnnotationTypeStreets)
    {
        NSString *identifier = @"Streets";
        iCodeBlogAnnotationView *newAnnotationView = (iCodeBlogAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (newAnnotationView == nil)
        {
            newAnnotationView = [[[iCodeBlogAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier]autorelease];
        }

        annotationView = newAnnotationView;
    }

    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];

    return annotationView;
}

#pragma mark tableview dataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView 
              titleForHeaderInSection:(NSInteger)section
{
    if (section == iCodeBlogAnnotationTypeHotels)
    {
        return @"Hotels";
    }
    else if (section == iCodeBlogAnnotationTypeAerialShots)
    {
        return @"AerialShots";
    }
    else if (section == iCodeBlogAnnotationTypeStreets)
    {
        return @"Streets";
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

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

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier]autorelease];
    }

    // here we are creating an array object BUT it is empty (it has no object in it)
    NSMutableArray *annotations = [[NSMutableArray alloc] init];

    if(indexPath.section == 0)
    {
        for(iCodeBlogAnnotation *annotation in [mapView annotations])
        {
            if([annotation annotationType] == iCodeBlogAnnotationTypeHotels)
            {
                // here is the line where we actually add objects to the array
                [annotations addObject:annotation];
            }
        }

        if ([annotations count] > indexPath.row)
        {    
            cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
        }
        else
        {
            cell.textLabel.text = @"Empty";
        }
    }
    else if (indexPath.section == 1)
    {
        for(iCodeBlogAnnotation *annotation in [mapView annotations])
        {
            if([annotation annotationType] == iCodeBlogAnnotationTypeAerialShots)
            {
                [annotations addObject:annotation];
            }
        }

        if ([annotations count] > indexPath.row)
        {
            cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
        }
        else
        {
            cell.textLabel.text = @"Empty";
        }
    }
    else if (indexPath.section == 2)
    {
        for(iCodeBlogAnnotation *annotation in [mapView annotations])
        {
            if([annotation annotationType] == iCodeBlogAnnotationTypeStreets)
            {
                [annotations addObject:annotation];
            }
        }

        if ([annotations count] > indexPath.row)
        {
            cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
        }
        else
        {
            cell.textLabel.text = @"Empty";
        }
    }

    return cell;
}
4

3 回答 3

0

在这种情况下,您访问一个数组:

cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];

并且注释数组可能是空的!

于 2013-04-05T21:16:51.090 回答
0

当您创建一个对象时,NSMutableArray *annotations = [[NSMutableArray alloc] init];它存在,但它是空的。也就是说,它里面没有对象。您只是在这一行中将对象添加到数组中:[annotations addObject:annotation];,您很可能永远也无法做到。也就是说,您最终只会添加正确的annotations 中尽可能多的对象。[mapView annotations]annotationType

然后,您盲目地访问数组的特定元素,而不检查是否有足够的元素。

至少,您应该替换这一行:

cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];

像这样:

if ([annotations count] > indexPath.row) {
    cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
}
else {
    cell.textLabel.text = @"Empty";
}

更好的是,你应该改变你的实现

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

确保您永远不会说一个部分中的行数多于要放入这些行的实际注释。

于 2013-04-06T00:59:34.733 回答
0

这是一个坏主意。在 viewDidLoad 中初始化你的数组(否则每次有一个单元格时你都会这样做)。在表执行此操作之前,在 viewDidLoad 中加载具有值的数组。如果您不能添加值,则放置一个 if 语句,确保如果数组大小为零,则不要执行任何将访问数组的代码

于 2013-04-06T04:18:59.700 回答