我有一个使用UITableview
应用程序的应用程序。如果我点击UITableViewcell
那个带来 JSON 数据。我创建了一个UIViewController
来显示在UITableView
. 我的要求是,如果我单击UITableview
单元格,相应的 PDF 文件将显示在模拟器中。我也开发了代码。但是模拟器上不会显示pdf文件。我是编程新手。我的疑问是如何使用 json 解析器下载 PDF 文件。请给我任何想法或向我发送任何示例代码。提前致谢这是我的 PDF 网址:
这是我的示例代码。
JSONPARSERPDFVIEWCONTROLLER .M
-(void)loadData
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
connect= [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
// delegate methods in json
-(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse *)response
{
[_data setLength:0];
NSLog(@"didReceiveResponse called");
}
//connection did receive data
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data
{
[_data appendData:data];
}
/ /connection did finish loading
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *jsonError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&jsonError];
if ([jsonObject isKindOfClass:[NSArray class]])
{
//NSArray *jsonArray = (NSArray *)jsonObject;
}
else if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSArray *array=[[NSArray alloc]init];
array=[jsonDictionary objectForKey:@""];
dataDictionary=[array objectAtIndex:0];
NSLog(@"%@",dataDictionary);
}
[urlsArray addObject:[dataDictionary objectForKey:@"UIViewController"]];
NSLog(@"%@",urlsArray);
//[self dataLoadingFinished:responseString withData:downloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//[connection release];
// = nil;
NSLog(@"didFailWithError called");
}
合适的视图.m
- (void)viewDidLoad
{
[super viewDidLoad];
fileNameList = [[NSArray alloc]initWithObjects:@"UIViewController",@"QLPreview class",@"UIDocumentInteractionController", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [fileNameList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [fileNameList objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
jsonparser *jParser=[[jsonparser alloc]init];
jParser.selectedIndex=indexPath.row;
[self.navigationController pushViewController:jParser animated:YES];
}