iOS Document preview feature tutorial

Source: Internet
Author: User

this article reprinted to http://blog.csdn.net/devday/article/details/6580444Document Iosuinavigationcontrollerextensionmicrosoftcomponents

What is the preview feature of the technical documentation in the iOS 4 SDK? Is the preview feature when you print the file. It is used in quicklook.framework, which supports document formats such as: IWork documents, Microsoft Office, Rich Text format, PDF, images, Text files and comma-s eparated (CSV) files.

Show a demo today showing its usage:

First step: Create a View-based project and join Quicklook.framewrok

Step Two: Modify the controller's header file as follows:

    1. #import <QuickLook/QuickLook.h>
    2. @interface Testviewcontroller:uitableviewcontroller <QLPreviewControllerDataSource>
    3. {
    4. Nsarray *arrayofdocuments;
    5. }
    6. @end

Modify the controller execution file as follows

  1. #import "TestViewController.h"
  2. @implementation Testviewcontroller
  3. #pragma mark-
  4. #pragma mark Initialization
  5. /*---------------------------------------------------------------------------
  6. *
  7. *--------------------------------------------------------------------------*/
  8. -(ID) init
  9. {
  10. if (self = [super init])
  11. {
  12. Arrayofdocuments = [[Nsarray alloc] Initwithobjects:
  13. @ "iosdevtips.png", @ "Remodel.xls", @"Core j2me technology.pdf", nil];
  14. }
  15. return self;
  16. }
  17. /*---------------------------------------------------------------------------
  18. *
  19. *--------------------------------------------------------------------------*/
  20. -(void) Loadview
  21. {
  22. [Super Loadview];
  23. [Self settitle:@"Files Available for Preview"];
  24. }
  25. #pragma mark-
  26. #pragma Mark Table Management
  27. Customize the number of sections in the table view.
  28. /*---------------------------------------------------------------------------
  29. *
  30. *--------------------------------------------------------------------------*/
  31. -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
  32. {
  33. return 1;
  34. }
  35. /*---------------------------------------------------------------------------
  36. *
  37. *--------------------------------------------------------------------------*/
  38. -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
  39. {
  40. return [arrayofdocuments Count];
  41. }
  42. /*---------------------------------------------------------------------------
  43. *
  44. *--------------------------------------------------------------------------*/
  45. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
  46. {
  47. static NSString *cellidentifier = @"TableRow";
  48. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
  49. if (cell = = nil)
  50. cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier] Autorelease];
  51. // ???
  52. [[Cell Textlabel] settext:[arrayofdocuments ObjectAtIndex:indexPath.row];
  53. [Cell Setaccessorytype:uitableviewcellaccessorydisclosureindicator];
  54. return cell;
  55. }
  56. /*---------------------------------------------------------------------------
  57. *
  58. *--------------------------------------------------------------------------*/
  59. -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
  60. {
  61. //When user taps a row, create the preview controller
  62. Qlpreviewcontroller *previewer = [[[[Qlpreviewcontroller alloc] init] autorelease];
  63. //Set data source
  64. [Previewer setdatasource:self];
  65. //which item to preview
  66. [Previewer SetCurrentPreviewItemIndex:indexPath.row];
  67. //Push new Viewcontroller, previewing the document
  68. [Self Navigationcontroller] pushviewcontroller:previewer Animated:yes];
  69. }
  70. #pragma mark-
  71. #pragma Mark Preview Controller
  72. /*---------------------------------------------------------------------------
  73. *
  74. *--------------------------------------------------------------------------*/
  75. -(Nsinteger) Numberofpreviewitemsinpreviewcontroller: (Qlpreviewcontroller *) controller
  76. {
  77. return [arrayofdocuments Count];
  78. }
  79. /*---------------------------------------------------------------------------
  80. *
  81. *--------------------------------------------------------------------------*/
  82. -(ID <QLPreviewItem>) Previewcontroller: (Qlpreviewcontroller *) controller previewitematindex: (Nsinteger) Index
  83. {
  84. //Break the path into it's components (filename and extension)
  85. Nsarray *filecomponents = [[arrayofdocuments objectatindex:index] componentsseparatedbystring:@"."];
  86. //use of the filename (index 0) and the extension (index 1) to get path
  87. NSString *path = [[NSBundle mainbundle] pathforresource:[filecomponents objectatindex:0] OfType:[fileComponents Objectatindex:1]];
  88. return [Nsurl Fileurlwithpath:path];
  89. }
  90. #pragma mark-
  91. #pragma Mark Cleanup
  92. /*---------------------------------------------------------------------------
  93. *
  94. *--------------------------------------------------------------------------*/
  95. -(void) Dealloc
  96. {
  97. // free up all the documents
  98. [Arrayofdocuments release];
  99. [Super Dealloc];
  100. }
  101. @end

Modify Appdelegate as follows

  1. -(void) applicationdidfinishlaunching: (uiapplication *) Application
  2. {
  3. //Create and initialize the window
  4. window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds];
  5. //Create Test View Controller
  6. VC = [[Testviewcontroller alloc] init];
  7. //Create navigation controller
  8. nav = [[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:VC];
  9. [Window Addsubview:[nav view];
  10. [Window makekeyandvisible];
  11. }

The required resource files can be found in the source code.

iOS Document preview feature tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.