开发者

UITableView that links Cell to UIWebView NSURLRequest

My question seems simple enough and yet I have not been able to find an example to follow. I have a UITableView with a list of courses that I want to link to their respective URLs. There are 21 courses in the list and when the user scrolls the list, they select a course that then should link them to the web page (with a "back" button to return to the list).

I've found examples to link the URL directly from a button but not from a UITableView list. How can you link the Cell in the TableView list to the NSURL Request to then load the web page?

Anyone with an answer or a link to 开发者_JS百科a tutorial, would be very appreciated.


Check the delegate methods for UITableView:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

In your case you probably want to push another viewcontroller with a webview when the user taps a cell so you can allow the user to go back to the uitableview when he/she is done.


Make an data object called Link with two instance variables, the URLRequest object and a string that you want for your table cell to display.

When you set up your 21 objects, make 21 Link objects and store them in an array.

In your tableView:cellForRowAtIndexPath: method you will setup your row with the linkInsance.cellName value. In your tableView:didSelectRowAtIndexPath: you will get the linkInstance.URLRequest and execute it.

You will get the link instances by getting an object by index in the array with the indexPath.row.


Use the standard "Navigation based application" template so that you've got a navigation controller in place. By default it is set up to use a table view controller as its root view controller, so it's a good starting point for you.

Now create a custom view controller subclass that embeds a UIWebView. Remember to create an IBOutlet for the webview and hook it up in Interface Builder. While you are in Interface Builder, select the main view and tell it you want a navigation bar at the top. Give your view controller an NSURL property too. In the loadView method for this class, tell the UIWebView to load that URL.

Now go back to the table view controller's delegate. In the tableView:didSelectRowAtIndexPath: method, create an autoreleased instance of your custom view controller. Assign the appropriate URL to the NSURL property that you just set up, then push it onto the navigation stack with [self.navigationController pushViewController:isAnimated:].

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜