打开表格视图单元格的URL点击
问题描述:
我想在单元格单击时打开特定的URL。打开表格视图单元格的URL点击
我想使用对象来做到这一点,但它不起作用。字符串'url'获取(null)。
代码:
//setting object
[self.url2 addObject:@"http://google.nl"];
//method to select cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get your url, eg: NSString *url = yourArray[indexPath.row];
NSString *url = [NSString stringWithFormat:@"%@", [self.url2 objectAtIndex:indexPath.row]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}
这甚至可能吗?
谢谢。
答
试试这个片断,
NSMutableArray *URLArray = [[NSMutableArray alloc] init];
[URLArray addObject:@"http://google.nl"];
NSString *URLString = [NSString stringWithFormat:@"%@",[URLArray objectAtIndex:indexPath.row]];
NSLog(@"URLString: %@",URLString.description);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString] options:@{} completionHandler:nil];
您可以在NSMutableArray
添加URLs
但要确保 URLArray.count == numberOfRowsInSection
避免NSRangeException
。
+1
查看最新评论。它是固定的 – Anoniem
为什么不是NSString * url = [self.url2 objectAtIndex:indexPath.row]; ? –
返回“访问不良”的原因 - 错误 – Anoniem
您可以发布所有代码吗? –