Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 13. Protocols > TablePrinter Example

TablePrinter Example

Let’s look at another example in detail. The TablePrinter class provides a table printing service for other objects. It prints a single column table of strings with an optional title and optional line numbers. A TablePrinter can create a table for any object as long as the object conforms to the <TablePrinterDataSource> protocol. This is an attractive way to do business. The TablePrinter itself never stores any of the data used in the table. It doesn’t have to know what class the data source is or anything about it, only that the data source can answer certain questions (via a message) like “How many rows are in this table?” or “What information goes in row j?”

Note

The TablePrinter class mimics, in a simplified fashion, the behavior of the AppKit’s NSTableView (on the desktop) and the UIKit’s UITableView (on the iPhone). NSTableView and UITableView are more complicated classes than TablePrinter, but they both get the data they display in the same way as a TablePrinter. They take a dataSource object that is expected to conform to a protocol and then query the data source using methods from the protocol.



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial