0

来自我的应用程序的编码 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;
}
4

2 回答 2

2
  • 首先,什么是Directory?查看您的第一个代码片段,它看起来像是Directory的子类UITableViewCell,但是您的第二个片段表明您没有使用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]]]

于 2011-04-28T02:49:05.940 回答
0

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

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

于 2011-04-28T02:41:36.310 回答