ios - 在表格 View 单元格上单击打开 URL

标签 ios objective-c uitableview 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

关于ios - 在表格 View 单元格上单击打开 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44660513/

相关文章:

android - 在 ipad/平板电脑上调整背景大小

ios - 具有自定义形状导航栏的 UINavigationController

ios - 增加 1 UITableViewCell 分隔符高度

ios - 当 tableview 内容大小增加时,scrollview 中的 tableview 然后 tableview 正下方的 textview 上升到 tableview

ios - 在 TabbarViewController 中隐藏特定 View Controller 中的状态栏

ios - UITableView Header Override (UIView) 滚动行为异常

objective-c - 在自定义纬度/经度处创建新的 CLLocationCoordinate2D

ios - applicationDidReceiveMemoryWarning 和 didReceiveMemoryWarning 之间有什么区别?

ios - 将高分辨率图像设置为 UIimageView 创建内存问题

iphone - 使用延迟创建一个返回的方法