【问题标题】:Open pdf files in webview according to tableview row selected根据选定的 tableview 行在 webview 中打开 pdf 文件
【发布时间】:2015-11-01 03:03:43
【问题描述】:

我从一个 sqlite 数据库中检索所有数据。我可以让他们填充表格视图。当我单击一行时,webview 打开但不显示 pdf 文件。视图只是纯白色背景。没有崩溃,没有错误,根本不显示任何东西。 这是我的代码。不知道我做错了什么。

#import "PdfTableVC.h"
#import "PdfVC.h"

@interface PdfTableVC ()

@end

@implementation PdfTableVC {
NSMutableArray *listOfPdf;
}

@synthesize pdfTable;

- (void)viewDidLoad {
[super viewDidLoad];
[self initDatabase];
[self getPoints];
}

#pragma mark - View lifecycle

-(void)initDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"my.db"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
    return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"my.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
{
    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}

-(void)getPoints{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"my.db"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{

    const char *sql = "SELECT * FROM file_geositi";

    sqlite3_stmt *searchStatement;


    if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK)
    {

        listOfPdf = [[NSMutableArray alloc] init];


        while (sqlite3_step(searchStatement) == SQLITE_ROW)
        {

            NSString *pdf = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement,0)];


            [listOfPdf addObject:pdf];


        }
    }
    sqlite3_finalize(searchStatement);
}
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listOfPdf count];
}

//Table View cell
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier =@"pdfCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil){
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [listOfPdf objectAtIndex:indexPath.row];

return cell;
}

//Detail View items
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showPDF"]) {
    NSIndexPath *indexPath = [self.pdfTable indexPathForSelectedRow];
     PdfVC *destViewController = (PdfVC *)segue.destinationViewController;
     destViewController.dataPdf= [listOfPdf objectAtIndex:indexPath.row];    
 }
}

PdfVC.h

#import <UIKit/UIKit.h>

@interface PdfVC : UIViewController

//UIWebView
@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property (nonatomic, strong) NSString *dataPdf;

@end

PdfVC.m

#import "PdfVC.h"

@interface PdfVC ()

@end

@implementation PdfVC
@synthesize webView;
@synthesize dataPdf;


- (void)viewDidLoad {
[super viewDidLoad];

//No sure about this and in fact makes the app crash   
NSString *pdfName = [NSString stringWithFormat:@"%@", [self.dataPdf description]];
NSString *path = [[NSBundle mainBundle] pathForResource:pdfName ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];


}


@end

有什么帮助吗?谢谢

【问题讨论】:

  • 在日志中追踪 PDF 的路径,看看是否获得了正确的 URL。请在上面的问题中粘贴示例 URL。
  • 日志只是说“按下单元格编号 1、2”等等...所有 pdf 文件都在一个名为 PDF 的文件夹中。
  • 好吧,在您进入 PDFVC 控制器之前,您必须放置一个 NSLog 语句并实际追踪 PDF 的路径。找出您在 dataPdf 属性中设置的值,看看您是否获得了 PDF 的正确路径。或者在将路径添加到 listOfPdf 数组时执行此操作。先看看你是否找到了正确的路径。
  • 还有你如何将 PDF 加载到 webview 中的代码。
  • 代码只有这一个。另外我正在放置属性(非原子,强) IBOutlet UIWebView *webView;属性(非原子,强) NSString *dataPdf;在 PDFVC 控制器中。我显然在这里做错了什么或遗漏了一些东西。

标签: objective-c uitableview pdf uiwebview


【解决方案1】:

好的,所以看了你的代码后,我发现了以下几点:

  • 您将 PDF 名称存储在 SQLite 数据库中(我真的不知道 了解这样做的目的,但假设你需要) 名称“pdf_1”、“pdf_2”和“pdf_3”,但这些名称不匹配 在您的支持文件夹中使用您的实际 PDF 文件名 它们是“PDF 1”等。
  • 其次,您只是将从数据库中提取的 PDF 的名称传递给 PdfVC,这实际上什么都不做,您必须在此处实际将 PDF 加载到 webView,所以它实际上什么都不做。

解决方案:

  • 将您的 PDF 从“PDF 1”等重命名为“pdf_1”等,以便它们与存储在数据库中的实际名称匹配,或重命名数据库中的条目以匹配 PDF 的文件名。
  • 其次,将以下代码放入PdfVC 视图控制器的viewDidLoad 方法中:

    NSString *path = [[NSBundle mainBundle] pathForResource:dataPdf ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webView loadRequest:request];
    

这段代码所做的只是使用存储在属性dataPdf(通过segue)中的名称从资源中提取正确的PDF,然后将该PDF加载到webView中。

我建议您在这里重新考虑实际数据库的用途,因为您所做的只是将 PDF 的名称存储到表中。它可能根本不需要,您可以在 PDFTableVC 视图控制器中维护 PDF 名称的 NSArray

希望这能回答你的问题。

【讨论】:

  • 谢谢,它在这个例子中工作。在现实生活中,我需要展示 148 个 pdf 文件,这就是我使用数据库的原因。问题是在实际应用程序中我收到此错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[NSURL initFileURLWithPath:]: nil string parameter”当我选择表格视图单元格
  • 这意味着您的其中一个 PDF 要么与数据库和支持资源之间的名称不匹配,要么该 PDF 不存在。请仔细检查。谢谢
  • 好的,我已经尝试使用其他已损坏的文件,这可能就是我收到该错误的原因。
  • 有没有办法通过邮件或在 fb 上发布等方式发送 webview 的内容(在这种情况下为 pdf)?
  • PDF 文件的大小可能会有所不同,并且可能会变得很大,因此直接共享它们并不是一个好主意。我这样做的方法是将这些文件存储在服务器上,并将数据库中每个文件的 URL 存储在一个新列中。共享文件时,请共享 PDF 的 URL,然后可以从 URL 访问该 URL。希望这能回答您的问题。
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多