I'm looking at some Apple sample code using the UINib method for cellForRowAtIndexPath for UITableView:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *QuoteCellIdentifier = @"QuoteCellIdentifier";
QuoteCell *cell = (QuoteCell*)[tableView dequeueReusableCellWithIdentifier:QuoteCellIdentifier];
if (!cell) {
UINib *quoteCellNib = [UINib nibWithNibName:@"QuoteCell" bundle:nil];
[quoteCellNib instantiateWithOwner:self options:nil];
cell = self.quoteCell;
self.quoteCell = nil;
I don't quite understand the last two lines
cell = self.quoteCell;
self.quoteCell = nil;
Can someone explain what is happening in those last two lines? thanks.
You have to look at this line:
[quoteCellNib instantiateWithOwner:self options:nil];
That is saying to the NIB to instantiate with the current object as the owner. Presumably in your NIB you have set the file's owner class properly and have an IBOutlet
property in that class called quoteCell
. So when you instantiate the NIB it will set that property in your instance, i.e. it's setting self.quoteCell
to be the newly created cell.
But you don't want to keep the property pointing to that cell because you've just used it as a temporary variable to get access to the cell. So you set cell
to be self.quoteCell
so that you can then return it from that function. Then you don't need self.quoteCell
anymore so you get rid of it.
[By the way, I assume this is using ARC? Otherwise you will want to retain cell
and then autorelease it.]