Question

In the classic pattern below the returned cell from dequeueReusableCellWithIdentifier is always non nil. Why? Shouldn't we have to allocated some cell first then we can have some to be reused?

I am using a customize cell and it was created in the storyboard. (However, if I had used the default UITableViewCell ,the phenomena is same - the returned cell is still always non-nil).

Enviroment: Xcode 4.3.3 iOs 5.1

AlbumListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"];

//cell always non nil --- why??
if(cell == nil){
    cell = [[AlbumListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AlbumCell"];

}
Was it helpful?

Solution

If you're using storyboard and set the UITableViewCell's Identifier to the identifier to the one you are using in dequeueReusableCellWithIdentifier ("AlbumCell" in your case) in the storyboard file, the UITableView will always create cells for you. I guess this is a feature of storyboard. If the identifier can't be found in your storyboard, then you need to create cells manually.

OTHER TIPS

I know it is possible for the return of the cell to be nil and you should check for it. Usually this will not be the case as you should get a cell created for you from your identifier you supplied. If you for instance did not have the identifier correct and the pool was empty I believe that is the case where you would get nil and you could do an aloc init to create a new cell. Also I should add the prototype cell you create in xcode is what creates the cell and that is why it should start as non nil and you don't have to create one.

To better answer this go to iTunes and go to the course iPad and iPhone Application Development - Paul Hegarty - Stanford CS193p course. Go to lecture (25) 9. Table Views go to 26 minutes in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top