Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dequeueReusableCellWithIdentifier never returns nil

Tags:

I am using a custom UITableViewCell in my UITableView but the problem is that the cell is never nil when calling the dequeueReusableCellWithIdentifier. Why is this ?

- (void)viewDidLoad
{
    [super viewDidLoad];
    UINib *nib = [UINib nibWithNibName:@"PHResultTableViewCell" bundle: nil];
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"MyCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    PHResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (cell == nil)
    {
        PackageHolidayItem *obj=[[PackageHolidayItem alloc]init];
        obj= [totalArray objectAtIndex:indexPath.row];
        cell.packageHolidayItem = obj;
        [cell loadRow];

    }
    return cell;
}    
like image 669
Trine Haals Madsen Avatar asked May 05 '13 06:05

Trine Haals Madsen


People also ask

Why we use dequeueReusableCellWithIdentifier?

dequeueReusableCellWithIdentifier . Instead of creating every single cell and then selectively displaying them, we only create a handful of cells, enough to fill the screen and a little more. As we scroll, we reuse the cells offscreen, leading to a much more memory efficient task.

What is the use of dequeueReusableCellWithIdentifier in ios?

dequeueReusableCellWithIdentifier: Returns a reusable table-view cell object after locating it by its identifier.

What does DequeueReusableCell do?

DequeueReusableCell(NSString)Returns a reusable table view cell that was created with the given ReuseIdentifier.

Why we use DequeueReusableCell in swift?

This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you didn't register a class or nib file, this method returns nil .


2 Answers

Starting in iOS 5 when you use storyboards and your reuse identifier matches a prototype in your storyboard you will not get a nil returned from dequeueReusableCellWithIdentifier.

From Apple Doc:

Table View Programming Guide for iOS

Creating and Configuring a Table View

Populating a Dynamic Table View with Data

If the dequeueReusableCellWithIdentifier: method asks for a cell that’s defined in a storyboard, the method always returns a valid cell. If there is not a recycled cell waiting to be reused, the method creates a new one using the information in the storyboard itself. This eliminates the need to check the return value for nil and create a cell manually.

You can log the cell address to prove to your self they are being reused. But don't ship with the logging it will really slow up your table.

NSLog(@"Deque Cell %p", cell);

Better yet use breakpoint to log it.

enter image description here

$25 = 0x097f9850 <DDSImageSubtitleCheckedTableViewCell: 0x97f9850; baseClass = UITableViewCell; frame = (0 22; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x97f9740>>
$26 = 0x0a6a4a00 <DDSImageSubtitleCheckedTableViewCell: 0xa6a4a00; baseClass = UITableViewCell; frame = (0 66; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0xa6a4b50>>
$27 = 0x0a3ad250 <DDSImageSubtitleCheckedTableViewCell: 0xa3ad250; baseClass = UITableViewCell; frame = (0 110; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0xa3ad390>>
$28 = 0x0a3ae640 <DDSImageSubtitleCheckedTableViewCell: 0xa3ae640; baseClass = UITableViewCell; frame = (0 176; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0xa3ae780>>
$29 = 0x0972a370 <DDSImageSubtitleCheckedTableViewCell: 0x972a370; baseClass = UITableViewCell; frame = (0 220; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x972a340>>

If you just want the addresses

enter image description here

0x097f9850
0x0a6a4a00
0x0a3ad250
0x0a3ae640
0x0972a370
like image 167
GayleDDS Avatar answered Oct 05 '22 02:10

GayleDDS


It does not return nil because you have registered a nib for cell reuse ([[self tableView] registerNib:nib forCellReuseIdentifier:@"MyCell"];).

if dequeueReusableCellWithIdentifier: can't find a cell in the tableviews reuse queue it will instantiate a new one from the nib you have specified.

like image 25
Matthias Bauch Avatar answered Oct 05 '22 04:10

Matthias Bauch