【发布时间】: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