2011-04-28 18 views
0

從我的應用程序的編碼detailView具有分組表視圖。我希望應用程序撥打選定的電話號碼,或者發送文字或發送電子郵件(如果點擊電子郵件,並選擇地址後轉到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
  • 首先,什麼是Directory?看看你的第一個代碼片段,它看起來像DirectoryUITableViewCell的一個子類,但你的第二個片段表明你沒有使用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

edc1591。感謝你的方式從細胞工程中獲得價值完美。但是當我點擊一個數字時,它什麼都不做。我知道它不會打電話,因爲它不在實際的電話,但它應該給我一些錯誤的權利。以及它將如何知道如何發送短信,撥打電話或發送電子郵件或轉到地圖。我是否需要提供一些if語句或檢查什麼樣的價值被回收? – Hitz 2011-04-28 04:29:14

+0

我相信tel://協議只適用於iPhone。無論它是否會在iPod/iPad中發生錯誤,我都不知道。我會找到和iPhone進行測試。我不知道如何檢測單元格中的數據類型。我不太瞭解你的代碼來幫助解決這個問題。 – edc1591 2011-04-28 14:48:06

+0

在數據庫中,我的電話號碼存儲爲'1(234)567-8910'電子郵件的格式爲'[email protected]',地址顯示爲地址,城市,州,郵編和國家的組合。像'1 Infinite Loop,Cupertino,CA 95014 US'。 – Hitz 2011-04-28 16:35:23

0

didSelectRowAtIndexPath:你會使用indexPath以類似的方式,你在cellForRowAtIndexPath:沒有確定你需要做出你的決定,以什麼做(撥一個號碼的數據,發送短信,或地圖地址)。

tel:sms:不同,沒有mail:方案。你會想爲此使用MFMailComposeViewController。至於映射地址,您需要深入MapKit。但是如果你有一個緯度和經度,你可以很容易地顯示一個地圖的地圖和一個地點的註釋。

相關問題