0

I have a UITableView which as some rows. On selecting a row respective pdf should be displayed in detailview.detialview has UIWebView declared in it. I want to use for loop to directing to detailview. Below is sample code:

- (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];
}
Indrajeet
  • 5,490
  • 2
  • 28
  • 43
user2552751
  • 229
  • 1
  • 6
  • 18

1 Answers1

0

Modify your tableviewDelegate Function as follow

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(), ^{ }); This block is executing many times so I have removed it and replaced the code as above. I hope it will work for you

svrushal
  • 1,612
  • 14
  • 25