我有一个UITableView作为一些行。在选择行时,应在detailview.detialview中显示相应的pdf,并在其中声明UIWebView。我想使用for循环来指向detailview。以下是示例代码:
- (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];
}
答案 0 :(得分:0)
修改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(),^ {});这个块正在执行多次,所以我删除了它并替换了上面的代码。我希望它对你有用