在MKPinAnnotationView中显示UITableView

3

我正在尝试在MKPinAnnotationView的气泡中显示UITableView。我在我的故事板文件中添加了UITableViewController。我使用以下代码将leftAccessoryView分配给UITableView。

- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id < MKAnnotation >)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil; 

    NSString *annotationIdentifier = @"ZillowPropertyDetailsIdentifier"; 

    MKPinAnnotationView *propertyDetailsView = (MKPinAnnotationView *) [mv 
                                                            dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

    if (!propertyDetailsView) 
    {
        propertyDetailsView = [[MKPinAnnotationView alloc] 
                    initWithAnnotation:annotation 
                    reuseIdentifier:annotationIdentifier];

        // get view from storyboard 
        PropertyDetailsViewController *propertyDetailsViewController = (PropertyDetailsViewController *) [self.storyboard instantiateViewControllerWithIdentifier:@"PropertyDetailsIdentifier"];

        propertyDetailsView.leftCalloutAccessoryView = propertyDetailsViewController.view;

        [propertyDetailsView setPinColor:MKPinAnnotationColorGreen];
        propertyDetailsView.animatesDrop = YES; 
        propertyDetailsView.canShowCallout = YES; 

    }
    else 
    {
        propertyDetailsView.annotation = annotation;
    }

    return propertyDetailsView; 
}

物业详情视图控制器:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

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

    // Return the number of rows in the section.
    return 1;
}

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

    // Configure the cell...

    return cell;
}

当我点击这个pin时,它会崩溃并显示BAD_EXCEPTION或其他错误信息。

你的propertyDetailsViewController是否有效(非nil),并且有一个有效的视图? - Marco
让我尝试使用UIPopUPController并在其中显示tableview。 - azamsharp
你有没有可能在任何地方没有实例化UITableView? - Nate Flink
实际上,我认为您无法在标注中显示UITableView。我需要关闭标注,然后显示一个弹出窗口,然后在弹出窗口中显示UITableView。 - azamsharp
我思考了一下,结果发现在 iPhone 应用程序中显示 UITableView 并不是一个好主意。但对于 iPad,这样做是有意义的! - azamsharp
显示剩余2条评论
1个回答

0
你只是遇到了一个内存问题。当你进行操作时,
PropertyDetailsViewController *propertyDetailsViewController = (PropertyDetailsViewController *) [self.storyboard instantiateViewControllerWithIdentifier:@"PropertyDetailsIdentifier"];

这会返回一个自动释放的 PropertyDetailsViewController。稍后,您只需将其视图分配给标注附件视图,但没有人保留视图控制器。因此,当方法完成时,视图控制器的保留计数为零并且被释放。访问该视图控制器上的任何内容时,都会引发BAD_EXCEPTION。

实际上,BAD_EXCEPTION通常是内存问题(太多的释放,两个自动释放等)。可以通过激活“启用僵尸对象”标志来更好地跟踪它们。这使得对象不会完全被释放,因此您可以看到哪个对象失败。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接