【问题标题】:pdf is not shown in detailview on selecting a row in UITableView在 UITableView 中选择一行时,pdf 未在 detailview 中显示
【发布时间】:2014-03-18 08:43:22
【问题描述】:

我有一个 UITableView 作为一些行。在选择一行时,应在 detailview.detialview 中显示相应的 pdf,其中声明了 UIWebView。我想使用 for 循环指向详细视图。下面是示例代码:

- (void)viewDidLoad
{
 [super viewDidLoad];
arrCategorieslist=[[NSMutableArray alloc]initWithObjects:@"Total requests",@"Initiated",@"In process",@"Completed",@"Rejected",nil];
 NSURL *url1=[NSURL URLWithString:@"http://www.........pdf"];
 NSURL *url2=[NSURL URLWithString:@"http://www.........pdf"];
 NSURL *url3=[NSURL URLWithString:@"http://www.........pdf"];
 NSURL *url4=[NSURL URLWithString:@"http://www.........pdf"];
 NSURL *url5=[NSURL URLWithString:@"http://www.........pdf"];

 arrUrl=[[NSMutableArray alloc]initWithObjects:url1,url2,url3,url4,url5,nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrCategorieslist count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
cell.textLabel.text = [arrCategorieslist objectAtIndex:indexPath.row];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
PdfViewController *displayPdf=[[PdfViewController alloc]init];
displayPdf.title=[arrCategorieslist objectAtIndex:indexPath.row];
dispatch_async(dispatch_get_main_queue(), ^{
int i;
for (i=0;i<[arrUrl count];i++) {

NSURLRequest *request=[NSURLRequest requestWithURL:[arrUrl objectAtIndex:indexPath.row]];
   NSLog(@"%@",[arrUrl objectAtIndex:i]);
    [displayPdf.webView loadRequest:request];
}
});
[self.navigationController pushViewController:displayPdf animated:YES];

}
PdfViewController.m……..
- (void)viewDidLoad
{
[super viewDidLoad];
webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320,480)];

webView.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin);
webView.scalesPageToFit=YES;
webView.delegate=self;
[self.view addSubview:webView];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

webView.delegate = self; // setup the delegate as the web view is shown
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

[webView stopLoading];   // in case the web view is still loading its content
webView.delegate = nil;  // disconnect the delegate as the webview is hidden
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
                         @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font><center></html>",
                         error.localizedDescription];
[self.webView loadHTMLString:errorString baseURL:nil];
}

【问题讨论】:

    标签: objective-c uitableview uiwebview uiwebviewdelegate


    【解决方案1】:

    如下修改你的 tableviewDelegate 函数

    PdfViewController *displayPdf;//declared outside as if project is ARC then it is crashing
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
         displayPdf=[[PdfViewController alloc]init];
         displayPdf.title=[arrCategorieslist objectAtIndex:indexPath.row];
    
        [self.navigationController pushViewController:displayPdf animated:YES];
    
         NSURLRequest *request=[NSURLRequest requestWithURL:[arrUrl objectAtIndex:indexPath.row]];
        [displayPdf.webView loadRequest:request];
    
     }
    

    dispatch_async(dispatch_get_main_queue(), ^{ });这个块执行了很多次,所以我删除了它并替换了上面的代码。希望对你有用

    【讨论】:

    • 它不工作,pdf不显示在detailview中。如果使用dispatch_asyc块则显示pdf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多