0

新しい編集に注意してください!!!!

そのため、配列から注釈を読み込んで地図上に表示するアプリを作成しようとしています...次に、左側の UITable で場所を選択すると、右側の地図上の場所にズームします。以下は、注釈をロードし、(UITable に表示される) セクション数を設定し、各セクションの行数を設定し、最後に注釈名を UITable に書き込んで、セレクターとして使用できるようにするためのコードです。

問題: アプリは実行されますが、左側の UITable (場所の選択に使用) では、ホテル セクションに何も表示されず、AerialView セクションに「空」という単語が表示され、街路セクションに 1 つの場所と残りの行が表示されます。 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

オブジェクトを作成すると、オブジェクトはNSMutableArray *annotations = [[NSMutableArray alloc] init];存在しますが、空です。つまり、オブジェクトが含まれていません。この行の配列にオブジェクトを追加するだけです: [annotations addObject:annotation];、これには決して到達しない可能性があります。つまり、正しいの にあるannotationのと同じ数だけオブジェクトを追加することになります。[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 に答える