0

From coded detailView of my app which has a grouped table view. I want app to dial selected phone number or send text or send email if clicked on email and take to google maps if address is selected. this is my didSelectRowAtIndexPath.. and its not working. i don't know how to get info of selected cell. Please help.

- (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"]]
}

And here is my cellForRowAtIndexPath if it helps.

- (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;
}
edc1591
  • 10,146
  • 6
  • 40
  • 63
Hitz
  • 133
  • 1
  • 10

2 Answers2

2
  • First off, what is Directory? Looking at your first code snippet, it looks like Directory is a subclass of UITableViewCell, but then your second snippet indicates that you are not using a UITableViewCell subclass.

    Change this line:

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

    To this:

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    Then you can get the values of the UITableViewCell like this:

    NSString *myString = cell.detailTextLabel.text

I'll leave it for you to determine what type of information is stored in the cell because you know better how your app works.

  • To make a call do this:

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

  • To send a text do this:

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

  • To open maps do this:

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

  • To send an email do this:

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

edc1591
  • 10,146
  • 6
  • 40
  • 63
  • edc1591. thanks ur way to get value from cell works perfect. but when i click on a number it does nothing. i know it wont call as its not on actual phone but it should give me some error right. and also how will it know what to do like to send sms or make a call or email or go to maps. do i need to put some if statement or check what kind of value is retrived? – Hitz Apr 28 '11 at 04:29
  • I believe the tel:// protocol will only work on an iPhone. Whether or not it will throw an error in an iPod/iPad, I don't know. I would find and iPhone to test on. I don't know how to detect what type of data is in the cell. I don't know enough about your code to help with that. – edc1591 Apr 28 '11 at 14:48
  • in database i've phone numbers stored as '1 (234) 567-8910' email has regular format 'one@apple.com' and address is displayed with a combination of address,city,state,zip and country. like '1 Infinite Loop, Cupertino, CA 95014 US'. – Hitz Apr 28 '11 at 16:35
  • For phone numbers look here: http://stackoverflow.com/questions/3349494/objective-c-regex-to-check-phone-number `NSPredicate` can also be used to check if it's an email. Then if it's not an email or phone number assume it's an address – edc1591 Apr 28 '11 at 19:00
  • Thanks a bunch edc1591. this and some if statements should make it work. Thanks again. – Hitz Apr 28 '11 at 20:48
0

In didSelectRowAtIndexPath: you would use indexPath in a similar manner as you did in cellForRowAtIndexPath: to determine the data you need to make your decision as to what to do (dial a number, send an SMS message, or map an address).

Unlike tel: and sms:, there is no mail: scheme. You'll want to use MFMailComposeViewController for that. And as for mapping an address, you'll need to dig into MapKit. But you can pretty easily display a map and a pin annotation for a location, provided you have a latitude and longitude for it.

Mark Granoff
  • 16,878
  • 2
  • 59
  • 61