【问题标题】:How to make a call, send text or find address on map?如何拨打电话、发送短信或在地图上查找地址?
【发布时间】:2011-08-14 08:16:20
【问题描述】:

来自我的应用程序的编码 detailView,它有一个分组表视图。如果单击电子邮件,我希望应用程序拨打选定的电话号码或发送文本或发送电子邮件,如果选择了地址,则转到谷歌地图。 这是我的 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;
}

【问题讨论】:

    标签: cocoa-touch ios uitableview openurl


    【解决方案1】:
    • 首先,Directory 是什么?查看您的第一个代码 sn-p,看起来 DirectoryUITableViewCell 的子类,但是您的第二个 sn-p 表明您没有使用 UITableViewCell 子类。

      改变这一行:

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

      到这里:

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

      然后你可以像这样得到UITableViewCell的值:

      NSString *myString = cell.detailTextLabel.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”,地址显示为地址、城市、州的组合,zip 和国家。比如“1 Infinite Loop, Cupertino, CA 95014 US”。
    • 电话号码请看这里:stackoverflow.com/questions/3349494/… NSPredicate 也可用于检查是否为电子邮件。然后,如果不是电子邮件或电话号码,则假定它是地址
    • 非常感谢 edc1591。 this 和一些 if 语句应该使它起作用。再次感谢。
    【解决方案2】:

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

    tel:sms: 不同,没有mail: 方案。您需要为此使用MFMailComposeViewController。至于映射地址,您需要深入研究 MapKit。但是你可以很容易地显示一个位置的地图和一个大头针注释,只要你有一个纬度和经度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多