One Quickie


Using a nib for a tableview cell (UITableView->General)
You can use a nib file for table view cells pretty easily in iOS5.

1) Make a nib with a single object at the top, a UITableViewCell. Lay it out as you wish. I use view tags to get to the objects contained therein.

enum {
    kTermLabelTag = 1,
    kDetailLabelTag = 2
};

2) Register the nib for a cell reuse identifier. I do it in my -viewDidLoad.

static NSString *g_cellReuseIdentifier = @"BWLookupCellReuseIdentifier"; // file global

...

    UINib *nib = [UINib nibWithNibName: @"BWLookupTableViewCell"  bundle: nil];
    [self.resultsView registerNib: nib
         forCellReuseIdentifier: g_cellReuseIdentifier];
3) Dequeue the cell as usual
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier: g_cellReuseIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc]
                   initWithStyle: UITableViewCellStyleSubtitle
                   reuseIdentifier: g_cellReuseIdentifier];
    }
4) Dig into the cell with the tags, and go nuts
    UILabel *termLabel = (UILabel *)[cell viewWithTag: kTermLabelTag];
    UILabel *detailLabel = (UILabel *)[cell viewWithTag: kDetailLabelTag];

    termLabel.text = termString;
    detailLabel.text = definition;



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com