【问题标题】:how to link static cells to Outlet/Actions - E.G tap the call cell to call the number如何将静态单元格链接到 Outlet/Actions - 例如点击呼叫单元以呼叫号码
【发布时间】:2026-02-08 11:45:01
【问题描述】:

我的代码。顺便说一句,我无法接听上班电话,网站也打不开!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlToOpen = @"";
if( indexPath.section == 0 ){
    // call number
    urlToOpen = @"tel:442074036933";
} else if( indexPath.section == 1 ){
    // open website
    urlToOpen = @"http://www.designmuseum.org";
} else {
    // open address

    urlToOpen = @"http://maps.apple.com/maps?daddr=Design+Museum+London";
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
NSString *destinationAddress = [NSString stringWithFormat:@"%@+%@+%@",
                                [address street],
                                [address city],
                                [address country]];

NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];

NSString *url = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%@&daddr=%@",
                 [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                 [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

我需要为我的应用添加目的地地址 = 28 Shad Thames, London, United Kingdom。用什么格式写?因为我无法让它工作,真的需要快速解决我的应用程序的这个问题

【问题讨论】:

    标签: uitableview nsstring uiapplication


    【解决方案1】:

    您不需要为每个单元创建插座。

    你的 didSelectRowAtIndexPath 方法被调用了吗?

    那么你可以这样做:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *urlToOpen = @"";
        if( indexPath.section == 0 ){
            // call number
            urlToOpen = @"tel:12125551212";
        } else if( indexPath.section == 1 ){
            // open website
            urlToOpen = @"http://www.google.nl";
        } else {
            // open address
                NSString *destinationAddress = @"Amsterdam";
    
                Class itemClass = [MKMapItem class];
                if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
    
                    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
                    [geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) {
                        if([placemarks count] > 0) {
    
                            MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];
    
                            MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];
    
                            MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];
    
    
                            NSArray *mapItems = @[mapItem, mapItem2];
    
                            NSDictionary *options = @{
                                            MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                            MKLaunchOptionsMapTypeKey:
                                            [NSNumber numberWithInteger:MKMapTypeStandard],
                                            MKLaunchOptionsShowsTrafficKey:@YES
                            };
    
                            [MKMapItem openMapsWithItems:mapItems launchOptions:options];
    
                        } else {
                            //error nothing found
                        }
                    }];
                    return;
                } else {
    
                    NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];
    
                    urlToOpen = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
                                 [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                                 [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                }
        }
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
    }
    

    indexPath.section 也可以是 indexPath.row,这取决于您制作 tableView 的方式。

    编辑: 我添加了“获取路线”部分。这将检查它是 ios5 还是 ios6。

    对于 ios5,我使用这篇文章中的 LocalizedCurrentLocation http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

    对于 ios6,我使用 CLGeocoder 获取地标,然后用它和当前位置打开地图。

    记得添加CoreLocation.framework和MapKit.framework

    【讨论】:

    • 需要 15 个我没有的声誉,不幸的是,最后一个问题,这个实现会打开地图应用程序,有没有办法在我的应用程序本身中做到这一点,而不是启动地图应用程序
    • 有可能,但您不能制作“获取路线”部分。您只能显示没有线路和导航等的位置和目的地。您必须创建一个地图视图。这是一个教程prassan-warrior.blogspot.dk/2012/11/…
    • 我得到了类似的东西,在我的应用程序 1 中,我的选项卡是一个地图视图,它显示了一个带有披露按钮的“设计博物馆伦敦”的地标我想实现与地图应用程序相同的功能显示信息 + 获取路线按钮,但希望在我的应用程序的地图视图中完成所有操作。
    • 这并不容易做到。这是地图的开始techotopia.com/index.php/…
    • 认为我会坚持打开地图应用程序,尽管理想情况下我希望在我自己的应用程序中使用它。它基本上是你帮助我的一切,只是没有启动地图
    最近更新 更多