Как позвонить, отправить текст или найти адрес на карте? - PullRequest
0 голосов
/ 28 апреля 2011

Из закодированного подробного вида моего приложения, в котором есть сгруппированное табличное представлениеЯ хочу, чтобы приложение набирало выбранный номер телефона или отправляло текст или отправляло электронную почту, если щелкнуть по электронной почте, и переходить на карты Google, если адрес выбран.это мой 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;
}

Ответы [ 2 ]

2 голосов
/ 28 апреля 2011
  • Прежде всего, что такое 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]]]

0 голосов
/ 28 апреля 2011

В didSelectRowAtIndexPath: вы будете использовать indexPath аналогично тому, как вы это делали в cellForRowAtIndexPath:, чтобы определить данные, необходимые для принятия решения о том, что делать (набрать номер, отправить SMS-сообщение или сопоставить адрес).

В отличие от tel: и sms:, схема mail: отсутствует. Вы хотите использовать MFMailComposeViewController для этого. А что касается сопоставления адреса, вам нужно копаться в MapKit. Но вы можете довольно легко отобразить карту и аннотацию булавки для местоположения, если у вас есть широта и долгота для нее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...