- The View Hierarchy
- Get a View to Draw Itself
- Drawing with NSBezierPath
- NSScrollView
- For the More Curious: Cells
- Challenge
For the More Curious: Cells
NSControl inherits from NSView. NSView is a relatively large and expensive object to create. When the NSButton class was first created, the first thing someone did was to create a calculator with 10 rows and 10 columns of buttons. The performance was less than it could have been because of the 100 tiny views. So someone had the clever idea of moving the brains of the button into another object (not a view) and creating one big view (called an NSMatrix) that would act as the view for all 100 button brains. The class for the button brains was NSButtonCell (Figure 12.19).
Figure 12.19 NSMatrix
NSButton became just a view that had an NSButtonCell. The button cell does everything, and NSButton simply claims a space in the window (Figure 12.20).
Figure 12.20 NSButton and NSButtonCell
Likewise, NSSlider is simply a view with an NSSliderCell, and NSTextField is a view with an NSTextFieldCell. NSColorWell, on the other hand, has no cell.
To create an instance of NSMatrix in Interface Builder, you drop a control with a cell onto the window and option-drag as if resizing until the matrix has the correct number of rows and columns (Figure 12.21).
Figure 12.21 A Matrix of Buttons
An NSMatrix has a target and an action. A cell may also have a target and an action. If the cell is activated, the cell's target and action get used. If the target and action of the selected cell are not set, the matrix's target and action will be used.
When dealing with matrices, it is common to ask which cell was activated. Cells can also be given a tag.
- (IBAction)myAction:(id)sender { id theCell = [sender selectedCell]; int theTag = [theCell tag]; ... }
The cell's tag can be set in Interface Builder.
Cells are used in several other types of objects. The NSTableView, for example, is filled with cells.