Reading custom table cell from nib file more than once
I am currently trying to load my custom table view cell from its own nib file instead of drawing the cell in code. It goes fine besides from the fact that I do not understand how I can make my table view contain more than one instance of my custom table cell. Let's see if I can explain my issue...
In my table view c开发者_JS百科ontroller I have something like IBOutlet MyFancyCell *fancyCell;
and then I make the controller the owner of MyFancyCell.xib
and connects the outlet in the controller to the table cell view in Interface Builder. Having
[[NSBundle mainBundle] loadNibNamed:@"MyFancyCell" owner:self options:nil];
in my controller fancyCell
ends up pointing to an instance of my fancy custom table view cell.
Now, what if I want two of those fancy cells in my table view?
You just use that IBOutlet temporarily to load your custom cell. In your cellForRowAtIndexPath
you do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyFancyCell";
MyFancyCell *cell = (MyFancyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"MyFancyCell" owner:self options:nil];
cell = fancyCell;
self.fancyCell = nil;
}
// configure cell...
return cell;
}
精彩评论