开发者

Make a row in UITableView point to a URL

I have a table which lists out items I would 开发者_JS百科to make each of the row in the table to point to a URL which is assigned to each item.


In your tableview delegate function didSelectRowAtIndexPath you can use this code to open a url in the Safari application.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSURL *url = [NSURL URLWithString:@"http://stackoverflow.com"]; 
    [[UIApplication sharedApplication] openURL:url];
}


If you don't want to leave your app, you can open the url in a UIWebView.

    UIWebView * wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];
    [self.view addSubview:wv];  // or push onto Navigation Stack
    // if adding wv as subview, also need to add a back button to self.view to dismiss webview.

    [wv loadRequest:
          [NSURLRequest  requestWithURL:
                 [NSURL   URLWithString:  myURLForSelectedRow]]];
    [wv autorelease];


When a cell is selected, the -tableview:didSelectRowAtIndexPath: delegate method will be called. From there, assuming you have the url for each row, the following code will open the URL url in Safari.

NSURL *url = /* Assume this exists */;
if( [[UIApplication sharedApplication] canOpenURL:url] )
{
    [[UIApplication sharedApplication] openURL: url];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜