来 self 的应用程序的编码 detailView,它有一个分组 TableView 。如果单击电子邮件,我希望应用程序调用选定的电话号码或发送文本或发送电子邮件,如果选择了地址,则转到谷歌地图。 这是我的 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;
}
最佳答案
首先,什么是
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]]]
要打开 map ,请执行以下操作:
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]]]
关于cocoa-touch - 如何调用电话、发送短信或在 map 上查找地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812818/