Issues on TableView reading Documents Folder

Discussion in 'iOS Programming' started by newtoiphonesdk, Sep 24, 2011.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I am setting up a blog app to save the webviews as HTML files in the app's Documents Directory:
    Code:
    -(IBAction)save {
    	self.titleofURL = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    	
    	NSString *urlString = _webView.request.URL.absoluteString;
    	
    	NSURL *url = [NSURL URLWithString:urlString];
    	
    	NSData *file = [NSData dataWithContentsOfURL:url];
    	
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	
    	NSString *documentsDirectory = [paths objectAtIndex:0];
    	
    	NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[self.titleofURL stringByAppendingString:@".html"]];
    	
    	[file writeToFile:pdfPath atomically:YES];
    	
    	
    	
    }
    
    I check the directory, and it does properly save the files. I then set up a new TableView Class with XIB to display the files in tableview using this code after setting an NSArray property in the .h file called 'files'
    Code:
    - (void)viewDidLoad {
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    	NSFileManager *manager = [NSFileManager defaultManager];
    	self.files = [manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
        [super viewDidLoad];
    	
    }
    #pragma mark -
    #pragma mark Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return 1;
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [self.files count];
    }
    
    
    // Customize the appearance of table view cells.
    - (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] autorelease];
        }
    	
    	cell.textLabel.text = [self.files objectAtIndex:indexPath.row];
    	
    	return cell;  
    }
    
    I have just two questions on how to go from here. First question: when I save a new article, and then go to the TableView, it will not show the file just saved until after I restart the app...How do I set it to reload the data when I view the TableView to search for new files that may be in there? Second question: what should I put in the didSelectRowAtIndexPath so that when I click on a file it opens up the webview and load that selected HTML in a UIWebView?
    Thanks
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    UITableView has an instance method just for this purpose (notice my bolded hints).
     
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Thanks, I was able to properly implement reloadData to take care of that problem, however, I am still having issues with displaying the HTML in a DetailView after I select the row.

    I have created a DetailViewController, with an NSString called selectedHtml. I import this class to my Tableview controller files. My didSelectRowAtIndexPath looks like this now.
    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	NSString *selectedHtml = [self.files objectAtIndex:indexPath.row];
    	DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    	dvController.selectedHtml = selectedHtml;
    	[self.navigationController pushViewController:dvController animated:YES];
    	[dvController release];
    	dvController = nil;
    	
    	 
    }
    I tested this without any loading code for the DetailView, and I was able to show the DetailView when I selected a path. This is my hangup though. If I understand this all, the NSString selectedHtml should be the HTML file that is saved. How do I tell the webview to load this string? I have tried different loadRequest methods, and can't get one to work, and all the documentation I have found for loading a local HTML involves first getting the path for everything, but it should just all me in my NSString, right?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    What is the relationship between your UIWebView and DetailViewController?
     
  5. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #5
    It is an iboutlet that I set up in the .h of detailviewcontroller.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Do you know how to set the HTML for a UIWebView when you have it in a string? Do you know how to set a property of an object? If you know both of these, it's just a matter of putting the two together then.
     
  7. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #7
    I haven't done any work with UIWebViews outside of loading a general URL and never anything within the app itself.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Well, what is your normal education approach when you know of a class but haven't really worked much with it before and wonder if it's capable of doing something?
     
  9. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #9
    I think I finally have it. Been pouring over documentation until I could hardly blink, but here is what I essentially have done. I have an IBAction in the original WebViewController that reads the blog articles. This saves an HTML in the Documents directory of the app. In the TableView's viewWillAppear I load
    Code:
    
    - (void)viewWillAppear:(BOOL)animated {
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    	NSFileManager *manager = [NSFileManager defaultManager];
    	self.files = [manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
    	[self.tableView reloadData];
        [super viewDidLoad];
        [super viewWillAppear:animated];
    	
    }
    
    In the rows section of table view I return self.files count.
    Then for didSelectRow part I have
    Code:
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	NSString *selectedHtml = [self.files objectAtIndex:indexPath.row];
    	DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    	dvController.selectedHtml = selectedHtml;
    	[self.navigationController pushViewController:dvController animated:YES];
    	[dvController release];
    	dvController = nil;
    	
    	 
    }
    
    Finally after setting up webView in a DetailViewController I ran the following:
    Code:
    
    - (void)viewDidLoad {
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	
    	NSString *documentsDirectory = [paths objectAtIndex:0];
    	
    	NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:selectedHtml];
    	
    	NSString *newHTMLString = [[NSString alloc] initWithContentsOfFile: pdfPath encoding: NSASCIIStringEncoding error: NULL];
    	
    	NSURL *newURL = [[NSURL alloc] initFileURLWithPath: pdfPath];
    	
    	[savedweb loadHTMLString: newHTMLString baseURL: newURL];
    }
    
    This retrieves the original path from where I saved it, and appends it with the name of the file, and then do the ASCII Encoding for the HTML and load it is HTML String. Finally for the test...ran the app, and it loaded up the saved view perfectly fine.
     

Share This Page