Graphics Programs Reference
In-Depth Information
- (void)viewDidLoad
{
[super viewDidLoad];
// Load the NIB file
UINib *nib = [UINib nibWithNibName:@"HomepwnerItemCell" bundle:nil];
// Register this NIB which contains the cell
[[self tableView] registerNib:nib
forCellReuseIdentifier:@"HomepwnerItemCell"];
}
Notice that we waited until viewDidLoad to register the XIB. If we registered it in the
designated initializer, the table view would only see this registration once. A low-memory
warning, in this situation, would effectively unregister this XIB file because a new in-
stance of UITableView would be created without re-registering it.
To get an instance of HomepwnerItemCell , all you do is ask the table view to dequeue
a cell for the HomepwnerItemCell reuse identifier. If the table has a reusable instance
of HomepwnerItemCell , it will return that. If it doesn't, it will load Homepwner-
ItemCell.xib and give you an instance of the archived cell. In ItemsViewCon-
troller.m , locate tableView:cellForRowAtIndexPath: and modify it like
so:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"];
}
BNRItem *p = [[[BNRItemStore sharedStore] allItems]
objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
// Get the new or recycled cell
HomepwnerItemCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"HomepwnerItemCell"];
// Configure the cell with the BNRItem
[[cell nameLabel] setText:[p itemName]];
[[cell serialNumberLabel] setText:[p serialNumber]];
[[cell valueLabel] setText:
[NSString stringWithFormat:@"$%d", [p valueInDollars]]];
return cell;
}
Build and run the application. Your cells should look like the cells in Figure 15.8 .
 
Search WWH ::




Custom Search