开发者

Make a row in UITableView point to a URL

开发者 https://www.devze.com 2023-03-18 18:47 出处:网络
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 didSelectRowAtIndexP

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

精彩评论

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