【问题标题】:Single PDF Viewer loading from multiple UITableView cells从多个 UITableView 单元格加载单个 PDF 查看器
【发布时间】:2013-10-15 23:27:12
【问题描述】:

我正在开发一个具有单个大型 UITableView 的项目,并且所有单元格都转到不同的 ViewController,每个单元格都显示一个单独的 PDF。但是,我想合并它,所以每个单元格都加载一个 PDF ViewController,它根据被点击的单元格加载相应的 PDF。这是我目前用于加载和显示 PDF 的代码。 在标题中:

    @property (weak, nonatomic) IBOutlet UIWebView *pdfView;

主要

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *urlAddress = [[NSString alloc] init];
        urlAddress = [[NSBundle mainBundle] pathForResource:@"PDF1" ofType:@"pdf"];
        NSURL *url = [NSURL fileURLWithPath:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [self.pdfView loadRequest:requestObj];
    }

我将如何修改此代码以让 PDFViewController 确定选择了哪个单元格并加载相应的 PDF? 谢谢, 尼克

【问题讨论】:

    标签: ios objective-c pdf uitableview


    【解决方案1】:

    在你的PDFViewController

    创建这样的属性@property(nonatomic, readwrite) NSString *selectedPDFUrl;

    在你的 tableViewContoller 有一个 NSMutableArray *arrayOfPDFStrings; 并将所有 URLS 添加到此数组中。

    当用户选择一个单元格时 'didSelectRowAtIndexPathcopy that particular cell PDF url into thePDFViewControllerobjectsselectedPDFUrl` 属性;

    -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {    
        PDFViewController*pdfView=[[PDFViewControlleralloc]initWithNibName:@"PDFViewControllers" bundle:nil];
        pdfView.selectedPDFUrl = [arrayOfPDfUrls objectAtIndex:indexPath.row - 1];
    
        [self.navigationController pushViewController:cards animated:YES];
    }
    

    完成此操作后,您可以通过此变量 selectedPDFUrl 访问 URL;

    所以你应该做这样的事情

    - (void)viewDidLoad
        {
            [super viewDidLoad];
            NSString *urlAddress = [[NSString alloc] init];
            urlAddress = [[NSBundle mainBundle] pathForResource:selectedPDFUrl ofType:@"pdf"];
            NSURL *url = [NSURL fileURLWithPath: selectedPDFUrl];
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
            [self.pdfView loadRequest:requestObj];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2021-03-21
      相关资源
      最近更新 更多