Question on code for making a new HomeownerItemCell


#1

In the code on page 303/304 we create a new custom item cell with one simple line:

However, the template that Xcode creates for table based apps seems to guard this with an “if” afterwards to determine if the deque request is nil:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }

The documentation for the dequeueReusableCellWithIdentifier method states that it will return…

[quote]Return Value
A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.
[/quote]

If this is true why are we simply asking to dequeue a cell and not checking for nil. Furthermore if it does return nil how are we then assigning values to it later in the method on p 304?


#2

My nascent understanding of this is that after having called registerNib:forCellReuseIdentifier: subsequent calls to dequeueReusableCellWithIdentifier: will never return nil and so the check for nil is unnecessary. The documentation for registerNib:forCellReuseIdentifier: states:

[quote] When you register a nib object with the table view and later call the dequeueReusableCellWithIdentifier: method, passing in the registered identifier, the table view instantiates the cell from the nib object if it is not already in the reuse queue.
[/quote]


#3

Your understanding may be nascent, but it is 100% correct.