allandriggers
Apple Fan
Profile
Posts: 188
Reg: Dec 13, 2012
Knoxville, TN
11,680
11/16/13 06:11 PM (12 years ago)

UITableView opening UIWebView Help

I have used buzztouch to build my first app, which is doing pretty well in the app store. I'm very thankful to a lot of people in helping me accomplish my goal. Now for my own learning I'm trying to re-create my app from scratch. I pretty much have everything figured out except how to load a pdf file that is embedded in my app from a tableview, and I'm hoping someone in here can help steer my in the right direction. What I have done is embedded 8 pdf files, a map, and a loan calculator. I created a new project in xcode 5, using the tab bar configuration. I'm using a tab bar to connect to my loan calculator and my map page. I want my home page to be a table view, and when pressed, displays a pdf file. In my .h file I have the following code:// tableViewController.h // Created by Allan Driggers on 11/8/13. // Copyright (c) 2013 Allan Driggers. All rights reserved. // #import <UIKit/UIKit.h @interface tableViewController : UIViewController { IBOutlet UIWebView *webView; } @property (nonatomic, retain) IBOutlet UITableView *tableView; @property(nonatomic,retain) IBOutlet UIWebView *webView; @end In my .m file: #import "tableViewController.h" @interface tableViewController () { NSArray *pageTitles; } @end @implementation tableViewController @synthesize tableView; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. pageTitles = [[NSArray alloc] initWithObjects: @"file1", @"file2", @"file3", @"file4", @"file5", @"file6", @"file7", @"file8", nil]; } #pragma mark #pragma mark table view data source -(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [pageTitles count]; } -(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =@"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil){ cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier]; if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } } cell.textLabel.text = [pageTitles objectAtIndex:indexPath.row]; return cell; } #pragma mark #pragma mark table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"Did Select Row"); NSLog(@"Row %d", indexPath.row); //so I can see if my selected rows are working int fileIndex = indexPath.row; NSArray *myPDFFiles=[[NSArray alloc]initWithObjects: @"file1.pdf", @"file2.pdf", @"file3.pdf", and so on... nil]; NSString *path = [[NSBundle mainBundle] pathForResource:[myPDFFiles objectAtIndex:fileIndex] ofType:@"pdf"]; NSURL *url = [NSURL fileURLWithPath:path]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [webView loadRequest:urlRequest]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end When I run it, it shows in my log that my selected rows are working, but the its not pulling up my second array of files. Should I create another class with an if/else statement? Use a switch statement? I'm feel like I'm so close but just can't get over the top. I need someone smarter that me to tell me what to do here. Thanks Allan
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
11/17/13 02:30 AM (12 years ago)
Looking at that code is making me woozy, so I won't dare say that I'm smarter than you. I usually get my success by seeing what someone has done before me. 'Those' are whom I consider to be the smarter guys… As such, snoop through the code for other plugins, like the HTML or PDF plugin, and see how they load their data… I noticed in the HTML plugin, that it gets the local file from the bundle, using a 'loadBundleData' method. It's similar to what you are doing, but your *path variable is a little different. David uses this: NSString *localFilePath = [BT_fileManager getBundlePath:[self saveAsFileName]]; NSURL *localURL = [NSURL fileURLWithPath:localFilePath]; NSURLRequest *URLReq = [NSURLRequest requestWithURL:localURL]; [webView loadRequest:URLReq]; which is darn close to what you're doing… so maybe make a small change in calling the file you want to open… NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[myPDFFiles objectAtIndex:fileIndex]]; I won't say that it'll work, but it's a shot… Cheers! -- Smug
 
allandriggers
Apple Fan
Profile
Posts: 188
Reg: Dec 13, 2012
Knoxville, TN
11,680
like
11/17/13 08:57 AM (12 years ago)
Hey Smug Yeah I tried using David's way of doing it first. His code is just bigger/more than I need. He is obviously thinking of a much bigger picture. So how is Guam? I would love to go back to Guam or Okinawa someday. It's so beautiful over there!!!
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
11/17/13 03:42 PM (12 years ago)
Oh well… I tried, lol! :) I may not have it right, but I kinda think that's where the code will be going. You might try throwing debugger statements all over to make sure your variables are what you think they should be during execution... Guam is great, I like it in the 'Winter' when the wind chill brings the temperature down to around 76 degrees or so… ;) Cheers! -- Smug
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.