SampleTableViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // SampleTableViewController.swift
  3. // PDFGenerator
  4. //
  5. // Created by Suguru Kishimoto on 2016/02/27.
  6. //
  7. //
  8. import UIKit
  9. import PDFGenerator
  10. class SampleTableViewController: UITableViewController {
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. self.tableView.contentInset = UIEdgeInsets(top: 100.0, left: 0.0, bottom: 0.0, right: 0.0)
  14. }
  15. override func didReceiveMemoryWarning() {
  16. super.didReceiveMemoryWarning()
  17. }
  18. @objc fileprivate func generatePDF() {
  19. do {
  20. let dst = NSHomeDirectory() + "/sample_tblview.pdf"
  21. try PDFGenerator.generate(self.tableView, to: dst)
  22. openPDFViewer(dst)
  23. } catch let error {
  24. print(error)
  25. }
  26. }
  27. fileprivate func openPDFViewer(_ pdfPath: String) {
  28. let url = URL(fileURLWithPath: pdfPath)
  29. let storyboard = UIStoryboard(name: "PDFPreviewVC", bundle: nil)
  30. let vc = storyboard.instantiateInitialViewController() as! PDFPreviewVC
  31. vc.setupWithURL(url)
  32. present(vc, animated: true, completion: nil)
  33. }
  34. // MARK: - Table view data source
  35. override func numberOfSections(in tableView: UITableView) -> Int {
  36. return 3
  37. }
  38. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  39. return 10
  40. }
  41. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  42. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SampleTableViewCell
  43. cell.leftLabel.text = "\((indexPath as NSIndexPath).section)-\((indexPath as NSIndexPath).row)cell"
  44. cell.rightLabel.text = "sample"
  45. return cell
  46. }
  47. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  48. return "section\(section)"
  49. }
  50. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  51. generatePDF()
  52. }
  53. /*
  54. // Override to support conditional editing of the table view.
  55. override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  56. // Return false if you do not want the specified item to be editable.
  57. return true
  58. }
  59. */
  60. /*
  61. // Override to support editing the table view.
  62. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  63. if editingStyle == .Delete {
  64. // Delete the row from the data source
  65. tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
  66. } else if editingStyle == .Insert {
  67. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  68. }
  69. }
  70. */
  71. /*
  72. // Override to support rearranging the table view.
  73. override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
  74. }
  75. */
  76. /*
  77. // Override to support conditional rearranging of the table view.
  78. override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  79. // Return false if you do not want the item to be re-orderable.
  80. return true
  81. }
  82. */
  83. /*
  84. // MARK: - Navigation
  85. // In a storyboard-based application, you will often want to do a little preparation before navigation
  86. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  87. // Get the new view controller using segue.destinationViewController.
  88. // Pass the selected object to the new view controller.
  89. }
  90. */
  91. }