首页 > 代码库 > iOS文档预览功能教程
iOS文档预览功能教程
本文转载至 http://blog.csdn.net/devday/article/details/6580444
文档iosuinavigationcontrollerextensionmicrosoftcomponents
ios 4 sdk中支技文档的预览功能,何为预览?就是你打印文件时的预览功能。其用到quicklook.framework,它支持的文档格式有: iWork documents, Microsoft Office, Rich Text Format, PDF, images, text files and comma-separated (csv) files.
今天show一个demo,展示其用法:
第一步:创建一个基于view的工程,并加入quicklook.framewrok
第二步:修改Controller的头文件如下:
- #import <QuickLook/QuickLook.h>
- @interface TestViewController : UITableViewController <QLPreviewControllerDataSource>
- {
- NSArray *arrayOfDocuments;
- }
- @end
修改 controller执行文件如下
- #import "TestViewController.h"
- @implementation TestViewController
- #pragma mark -
- #pragma mark Initialization
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- -(id)init
- {
- if (self = [super init])
- {
- arrayOfDocuments = [[NSArray alloc] initWithObjects:
- @"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];
- }
- return self;
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (void)loadView
- {
- [super loadView];
- [self setTitle:@"Files Available for Preview"];
- }
- #pragma mark -
- #pragma mark Table Management
- // Customize the number of sections in the table view.
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [arrayOfDocuments count];
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"tableRow";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- // ???
- [[cell textLabel] setText:[arrayOfDocuments objectAtIndex:indexPath.row]];
- [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
- return cell;
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // When user taps a row, create the preview controller
- QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
- // Set data source
- [previewer setDataSource:self];
- // Which item to preview
- [previewer setCurrentPreviewItemIndex:indexPath.row];
- // Push new viewcontroller, previewing the document
- [[self navigationController] pushViewController:previewer animated:YES];
- }
- #pragma mark -
- #pragma mark Preview Controller
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
- {
- return [arrayOfDocuments count];
- }
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
- {
- // Break the path into it‘s components (filename and extension)
- NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];
- // Use the filename (index 0) and the extension (index 1) to get path
- NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];
- return [NSURL fileURLWithPath:path];
- }
- #pragma mark -
- #pragma mark Cleanup
- /*---------------------------------------------------------------------------
- *
- *--------------------------------------------------------------------------*/
- - (void)dealloc
- {
- // Free up all the documents
- [arrayOfDocuments release];
- [super dealloc];
- }
- @end
修改Appdelegate如下
- - (void)applicationDidFinishLaunching:(UIApplication *)application
- {
- // Create and initialize the window
- window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Create test view controller
- vc = [[TestViewController alloc] init];
- // Create navigation controller
- nav = [[UINavigationController alloc] initWithRootViewController:vc];
- [window addSubview:[nav view]];
- [window makeKeyAndVisible];
- }
所要的资源文件可以源码中找到。
iOS文档预览功能教程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。