Cocoa touch 如何打电话、发短信或在地图上查找地址?
来自我的应用程序的编码详细视图,该应用程序具有分组表视图。我想应用程序拨打选定的电话号码或发送文本或发送电子邮件,如果点击电子邮件,并采取谷歌地图,如果地址被选中。 这是我的didSelectRowAtIndexPath。。而且它不起作用。我不知道如何获取所选单元格的信息。请帮忙Cocoa touch 如何打电话、发短信或在地图上查找地址?,cocoa-touch,ios,uitableview,openurl,Cocoa Touch,Ios,Uitableview,Openurl,来自我的应用程序的编码详细视图,该应用程序具有分组表视图。我想应用程序拨打选定的电话号码或发送文本或发送电子邮件,如果点击电子邮件,并采取谷歌地图,如果地址被选中。 这是我的didSelectRowAtIndexPath。。而且它不起作用。我不知道如何获取所选单元格的信息。请帮忙 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Directory
- (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;
}
在
didSelectRowAtIndexPath:
中,您将以与在cellForRowAtIndexPath:
中类似的方式使用indepath
来确定您需要的数据,以决定要做什么(拨打号码、发送短信或映射地址)
与电话:
和短信:
不同,没有邮件:
方案。为此,您需要使用MFMailComposeViewController
。至于映射地址,您需要深入研究MapKit。但是,只要有纬度和经度,就可以很容易地显示地图和位置的pin注释。- 首先,什么是
?查看第一个代码片段,它看起来像是目录
是Directory
的子类,但第二个代码片段表明您没有使用UITableViewCell
子类 更改此行:UITableViewCell
Directory*text=(Directory*)[tableView cellforrowatinexpath:indexPath]代码> 为此:
UITableViewCell*cell=[tableView cellforrowatinexpath:indexPath]代码> 然后您可以像下面这样获得
的值:UITableViewCell
NSString*myString=cell.detailtextlab.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]]]
NSPredicate
也可用于检查它是否是电子邮件。然后,如果不是电子邮件或电话号码,则假设它是一个地址感谢一堆edc1591。这和一些if语句应该可以让它工作。再次感谢。