Graphics Programs Reference
In-Depth Information
either of these properties strong references would create a retain cycle because a child (the
cell) would have a strong reference to its parent (the UITableView ) or its grandparent
(the controller).
In HomepwnerItemCell.m , synthesize these properties.
@synthesize controller;
@synthesize tableView;
Now we need to set these properties when the cell is created. In ItemsViewControl-
ler.m , locate the tableView:cellForRowAtIndexPath: method and add the
following code.
HomepwnerItemCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"HomepwnerItemCell"];
[cell setController:self];
[cell setTableView:tableView];
[[cell nameLabel] setText:[p itemName]];
Why do we need to give the cell a pointer to the table view when the controller already
has a pointer to that table view? Wouldn't it be simpler just to have the cell pass itself and
let the controller determine the table view? Yes, it would. But we're doing it this way so
that, later in the chapter, the cell can pass its own index path to enable some cool
Objective-C techniques. And serious bonus points for having these questions.)
Relaying the message to the controller
Every cell will now know its controller and the table view it is displayed on. When the
message showImage: is sent to a HomepwnerItemCell , we want the Homepwner-
ItemCell to turn around and tell the ItemsViewController to show the image for
the item at the cell's index path.
In HomepwnerItemCell.m , implement showImage: to get its index path from the
table view and then send its controller the showImage:atIndexPath: message.
(We'll get to the implementation of showImage:atIndexPath: in a moment.)
- (IBAction)showImage:(id)sender
{
NSIndexPath *indexPath = [[self tableView] indexPathForCell:self];
[[self controller] showImage:sender
atIndexPath:indexPath];
}
Search WWH ::




Custom Search