Welcome to the list and the platform, JD. The purpose of the cell reuse queue is so that we don't have to keep creating new cell objects as we scroll a table. We take advantage of the fact that as new cells are appearing, old cells are disappearing. Rather than throw the old cells away and create new ones, we reuse them. We can take advantage of this same principle in other situations besides scrolling, such as reloading.
So, to answer your question of why you enter the if statement more than once, it's because each cell on screen is still a unique cell object. Therefore we have to create at least as many cells as are on the screen. A cell does not enter the reuse queue until it goes off screen, and as long as there is nothing in the queue, attempts to dequeue a cell will return nil. If you were to scroll your table or reloadData, you would find instances where you did get a valid cell out of the queue. Luke Sent from my iPad On Feb 2, 2011, at 2:42 AM, JD Guzman <[email protected]> wrote: > Hello, > > I'm new to this list and also relatively new to development on the Mac/iPhone > platforms. > > I was just wondering if someone could clarify how exactly > dequeueReusableCellWithIdentifier: works. The reason I ask is because I have > the following code: > > --------- > > - (UITableViewCell *)tableView:(UITableView *)tableView > cellForRowAtIndexPath:(NSIndexPath *)indexPath { > > UITableViewCell *cell = [channelList > dequeueReusableCellWithIdentifier:@"channelCell"]; > > if (cell == nil) { > cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero > reuseIdentifier:@"channelCell"] autorelease]; > } > > [[cell textLabel] setText:[channels objectAtIndex:[indexPath row]]]; > > return cell; > } > > --------- > > Now if I set a break point inside the if statement I was expecting that it > would only be entered once, however it is entered every time a cell is > created. Shouldn't the call to dequeueReusableCellWithIdentifier: return a > cell after it has been created inside the if statement? Or am I > misunderstanding how this works? > > Regards, > > JD_______________________________________________ > > Cocoa-dev mailing list ([email protected]) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/luketheh%40apple.com > > This email sent to [email protected] _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
