Why is TableView not printing all rows?

Hi all, I have spent the last couple of days trying to solve this problem with no luck, so I'm asking for some quidance.


1. I have a long list of Objects that I have broken up into an array of separrate pages :[ObjectType]

2. I have a ViewController that has a TableView and a TextField to hold the page nuber being printed

3. I have a class property of objectsPerPage:Int. (example: 72)

4. For each page I wish to print I load the TableView with 72 objects out of the list of all objects and set the TextField to the pageNumber

5. I then have a loop when I can print the ViewController.view for any or all of the pages using:


let printOperation = NSPrintOperation(view: view, printInfo : printInfo)

...

printOperation.run()



No matter what range of pages I print or print all pages, the first page printed always has exactly 37 rows printed in the TableView. The remaining pages are print with the proper number of rows in the TableView, 72 in this case.


I have check the "NSTableViewDataSource.numberOfRows(in tableView: NSTableView) -> Int" function and for all pages including the first page printed it returns the correct value of "72"


I have checked the"NSTableViewDelegate.tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?" function and it is called (72 *number of Columns) for every page except the first which is correct. The fist Page printed is called (38 * number of Columns) which is wrong but also doesn't match the 37 rows that are being printed.


After the first page printed (incorrectly), there is no error mesage printed. But after the second page printed (correctly) and each subsequent page, the following error code is displayed:


2018-12-29 09:26:01.258290-0700 HCB_121918[25663:9040666] [Printing] -[NSPrintThumbnailView widthForHeight:]: Invalid result: nan (paperAspectRatio: nan, paperShadowOffset: {0, -3}, paperShadowBlurRadius: 6.000000)

2018-12-29 09:26:01.258315-0700 HCB_121918[25663:9040666] [Printing] -[NSPrintPreviewController _tileView]: Bailing due to invalid thumbnailViewSize: {nan, 310} (maxThumbnailViewSize: {310, 310})


This entire message is repeated 16 times after each correctly printed page.


I have searched my entire code for the number '37' and it does not appear anywhere

I have searched the Apple documentation for the class 'NSPrintPreviewController' but found nothing.


Any help to point me in the right direction would be most appreciated.


Thanks, Tom

Do you display the tableView in its view ?


Does the first page display the 72 items correctly ?


Could you post the code for printing ?

Claude31,


Thank you for replying.


1. No, I do not display NSView that contains the TableView.

2. The preview thumbnail in the print panel shows the first page incorrectly with 37 rows in the tableView. The rest of the pages correctly show 72 rows.


A page is a structure that contains the page number that is printed on each page and the 'entries' that are the data source for the TableView. 'EntryMO' is a CoreData managed object that that has 7 attributes:


struct registerPage {

var pageNumber = 0

var entries : [EntryMO] = []

}


The view of a single page to be print is a sub class of NSViewController


class PrintRegisterViewController: NSViewController {


//MARK: Properties


let DMS = DataManager.shared

let SDF = DateFormatter()

let CNF = NumberFormatter()



let entriesPerPage = 72


var startPageNumber = 0

var endPageNumber = 0



var entries : [EntryMO] = []




//MARK: Outlets


@IBOutlet weak var dateTextField: NSTextField!


@IBOutlet weak var pageTextField: NSTextField!


@IBOutlet weak var entriesTableView: NSTableView!


...



For each page to be printed ‘getPageEntries()’, 'loadTheView()' and 'printSinglePage()' are called. The vars 'firstEntry' and 'lastEntry' determine which group of 72 EntryMOs from CoreData make up the single page.


func getPageEntries(pageNumber: Int) -> [EntryMO]

{

var page : [EntryMO] = []

let firstEntry = pageNumber * entriesPerPage

var lastEntry = firstEntry + (entriesPerPage - 1)

if lastEntry > (DMS.entries.count - 1)

{

lastEntry = (DMS.entries.count - 1)

}

for entryNumber in firstEntry...lastEntry

{

let reversedEntries = DMS.entries.sorted(by: {$0.sequence < $1.sequence})

page.append( reversedEntries[entryNumber])

}


return page

}



func loadTheView(page: registerPage)

{

dateTextField.stringValue = SDF.string(from: Date())

pageTextField.stringValue = String(page.pageNumber)

entries = page.entries

entriesTableView.reloadData()

}


func printSinglePage( page : registerPage)

{

loadTheView(page: page)

let printInfo = NSPrintInfo()

printInfo.bottomMargin = 2

printInfo.topMargin = 2

printInfo.leftMargin = 0

printInfo.rightMargin = 2

printInfo.scalingFactor = 1.0

printInfo.orientation = .portrait

let printOperation = NSPrintOperation(view: view, printInfo : printInfo)

printOperation.printPanel.options.insert(NSPrintPanel.Options.Element.showsPaperSize)

printOperation.printPanel.options.insert(NSPrintPanel.Options.Element.showsOrientation)

printOperation.printPanel.options.insert(NSPrintPanel.Options.Element.showsScaling)

printOperation.printPanel.options.insert(NSPrintPanel.Options.Element.showsPreview)

printOperation.run()

}



The program is a banking checkbook program for my personal use only. I typically only print the last page or two as new transactions are downloaded as a CVS file from my bank.


Thank you for any help you can provide.

Why is TableView not printing all rows?
 
 
Q