Cocoa touch 如何打电话、发短信或在地图上查找地址?

Cocoa touch 如何打电话、发短信或在地图上查找地址?,cocoa-touch,ios,uitableview,openurl,Cocoa Touch,Ios,Uitableview,Openurl,来自我的应用程序的编码详细视图,该应用程序具有分组表视图。我想应用程序拨打选定的电话号码或发送文本或发送电子邮件,如果点击电子邮件,并采取谷歌地图,如果地址被选中。 这是我的didSelectRowAtIndexPath。。而且它不起作用。我不知道如何获取所选单元格的信息。请帮忙 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Directory

来自我的应用程序的编码详细视图,该应用程序具有分组表视图。我想应用程序拨打选定的电话号码或发送文本或发送电子邮件,如果点击电子邮件,并采取谷歌地图,如果地址被选中。 这是我的didSelectRowAtIndexPath。。而且它不起作用。我不知道如何获取所选单元格的信息。请帮忙

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Directory *text = (Directory *)[tableView cellForRowAtIndexPath:indexPath];

    //check content of text.
    NSLog(@"Phone Number Selected is %@", text);

    //NSString *dialHome = [dispHomephone stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:17148581499"]]
}
这是我的cellForRowAtIndexPath,如果有帮助的话

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    int cellPhoneLen = [dispCellphone length];
    int workPhoneLen = [dispWorkphone length];
    int homePhoneLen = [dispHomephone length];
    int emailLen = [dispEMail length];

    NSString *address = [NSString stringWithFormat:@"%@\n"@"%@ %@ %@\n"@"%@", dispAddress, dispCity, dispState, dispZip, dispCountry];
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    // Configure the cell...
    switch (indexPath.section) 
    {
        case 1:
        {
            if (homePhoneLen != 0)
            {
                switch (indexPath.row)
                {
                    case 0:
                    {
                        if (homePhoneLen != 0)
                        {
                            cell.textLabel.text = NSLocalizedString(@"home :", @"homephone label");
                            cell.detailTextLabel.text = dispHomephone;
                        } break;
                    case 1:
                        if (workPhoneLen != 0)
                        {
                            cell.textLabel.text = NSLocalizedString(@"work :", @"workphone label");
                            cell.detailTextLabel.text = dispWorkphone;
                        } break;
                    case 2:
                        if (cellPhoneLen != 0)
                        {
                            cell.textLabel.text = NSLocalizedString(@"mobile :", @"cellphone label");
                            cell.detailTextLabel.text = dispCellphone;
                        }
                    }
                }
            }

            else if (workPhoneLen != 0)
            {
                switch (indexPath.row)
                {
                    case 0:
                    {
                        if (workPhoneLen != 0)
                        {
                            cell.textLabel.text = NSLocalizedString(@"work :", @"workphone label");
                            cell.detailTextLabel.text = dispWorkphone;
                        } break;
                    case 1:
                        if (cellPhoneLen != 0)
                        {
                            cell.textLabel.text = NSLocalizedString(@"mobile :", @"cellphone label");
                            cell.detailTextLabel.text = dispCellphone;
                        }
                    }
                }
            }

            else if (cellPhoneLen != 0)
                {
                    cell.textLabel.text = NSLocalizedString(@"mobile :", @"cellphone label");
                    cell.detailTextLabel.text = dispCellphone;
                }
        } break;
        case 2:
        {
            if (emailLen != 0)
            {
                cell.detailTextLabel.text = dispEMail;
            }

        } break;
        case 3:
        {           
            cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.detailTextLabel.numberOfLines = 3;
            cell.detailTextLabel.text = address;
        } break;

    }
    return cell;
}

didSelectRowAtIndexPath:
中,您将以与在
cellForRowAtIndexPath:
中类似的方式使用
indepath
来确定您需要的数据,以决定要做什么(拨打号码、发送短信或映射地址)

电话:
短信:
不同,没有
邮件:
方案。为此,您需要使用
MFMailComposeViewController
。至于映射地址,您需要深入研究MapKit。但是,只要有纬度和经度,就可以很容易地显示地图和位置的pin注释。

  • 首先,什么是
    目录
    ?查看第一个代码片段,它看起来像是
    Directory
    UITableViewCell
    的子类,但第二个代码片段表明您没有使用
    UITableViewCell
    子类

    更改此行:

    Directory*text=(Directory*)[tableView cellforrowatinexpath:indexPath]

    为此:

    UITableViewCell*cell=[tableView cellforrowatinexpath:indexPath]

    然后您可以像下面这样获得
    UITableViewCell
    的值:

    NSString*myString=cell.detailtextlab.text

我将留给您来确定手机中存储的信息类型,因为您更了解应用程序的工作方式

  • 要拨打电话,请执行以下操作:

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@“tel://%@”,myString]]]

  • 要发送文本,请执行以下操作:

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@“sms://%@”,myString]]]

  • 要打开地图,请执行以下操作:

    myString=[addressText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString*urlString=[NSString stringWithFormat:@]http://maps.google.com/maps?q=%@“,myString];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlText]]

  • 要发送电子邮件,请执行以下操作:

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@“mailto://%@”,myString]]]


edc1591。感谢您的方式,从手机中获取价值,完美工作。但当我点击一个数字时,它什么也不做。我知道它不会打电话,因为它不是在实际的电话,但它应该给我一些错误的权利。还有,它如何知道该做什么,比如发送短信、打电话、发电子邮件或去地图。我是否需要输入一些if语句或检查检索的值是什么?我相信tel://协议只适用于iPhone。我不知道它是否会在iPod/iPad上出错。我会找到苹果和iPhone进行测试。我不知道如何检测单元格中的数据类型。我对您的代码了解不够,无法提供帮助。在数据库中,我将电话号码存储为“1(234)567-8910”,电子邮件格式为常规格式”one@apple.com'和地址显示为地址、城市、州、邮政编码和国家/地区的组合。如“1无限循环,加利福尼亚州库珀蒂诺95014 US”。有关电话号码,请查看此处:
NSPredicate
也可用于检查它是否是电子邮件。然后,如果不是电子邮件或电话号码,则假设它是一个地址感谢一堆edc1591。这和一些if语句应该可以让它工作。再次感谢。