Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cellForRowAtIndexPath returns nil

I'm trying to get a specific cell from a table view so I can change it's label and stop the activity indicator.

The problem I'm having is that cellForRowAtIndexPath returns nil.

My table view has only 1 row.

Code :

- (id) initWithNibName: (NSString*) nibNameOrNil bundle: (NSBundle*) nibBundleOrNil
{
    self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];

    if (self) 
    {
        numberOfRows = 1;
    }

    return self;
}

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section
{
    return numberOfRows;
}

-(void) stopCellIndicator
{

    LiveUserFeedCell* cell = (LiveUserFeedCell*)[self.liveFeed cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];  
    [cell.activityIndicator stopAnimating];
    cell.middleLabel.text = @"N/a";
}

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
    static NSString *CellIdentifier = @"UserFeedCell";


    LiveUserFeedCell *cell = (LiveUserFeedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"LiveUserFeedCell" owner:self options:nil];
        cell = (LiveUserFeedCell *)[nib objectAtIndex:0];
    }

    [cell.imgView setImage:[TUNinePatchCache imageOfSize:cell.imgView.frame.size forNinePatchNamed:@"bg_home_newsfeed_normal"]];


    if (shouldDisplayLoading == NO)
    {
        NSArray* songs = homeScreen.recentPlaybacks.songs;
        TWSong* song = [songs objectAtIndex:index];
        cell.middleLabel.text = @"";
        [cell.activityIndicator stopAnimating];

        UIFont *font = [UIFont fontWithName:@"Trebuchet MS" size:14];                       

        NSString* kText =[NSString stringWithFormat:@"<b>%@</b> is listening to %@ by %@",song.user,song.title,song.artist];

        for(int i = 14; i > 0; i=i-1)
        {
            // Set the new font size.
            font = [font fontWithSize:i];
            // You can log the size you're trying: NSLog(@"Trying size: %u", i);

            /* This step is important: We make a constraint box 
             using only the fixed WIDTH of the UILabel. The height will
             be checked later. */ 
            CGSize constraintSize = CGSizeMake(cell.isWatching.frame.size.width, MAXFLOAT);

            // This step checks how tall the label would be with the desired font.
            CGSize labelSize = [kText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

            /* Here is where you use the height requirement!
             Set the value in the if statement to the height of your UILabel
             If the label fits into your required height, it will break the loop
             and use that font size. */
            if(labelSize.height <= cell.isWatching.frame.size.height)
                break;
        }



        cell.isWatching.font = font;
        cell.isWatching.text = [TTStyledText textFromXHTML:kText lineBreaks:YES URLs:YES];
        cell.isWatching.textAlignment = UITextAlignmentCenter;

        ++index;
        if (index == [songs count])
            index=0;

    }
    else
    {       
        [cell.activityIndicator startAnimating];
    }

    return cell;
}

However, if I do something like this, I do get a valid cell:

NSArray *indexPaths = [NSArray arrayWithObjects: 
                       [NSIndexPath indexPathForRow:0 inSection:0],
                       nil];

numberOfRows = 0;
[self.liveFeed deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];


numberOfRows = 1;
[self.liveFeed insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];

LiveUserFeedCell* cell = (LiveUserFeedCell*)[self.liveFeed cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];  
[cell.activityIndicator stopAnimating];
cell.middleLabel.text = @"N/a";

Any idea how to fix this? (Maybe the default first cell isn't in row 0 section 0?)

like image 764
Idan Avatar asked Mar 08 '11 13:03

Idan


2 Answers

It is worth mentioning that cellForRowAtIndexPath: will also return nil if the cell is not visible.

This can especially be an issue when threading...

like image 119
Andres Canella Avatar answered Oct 21 '22 11:10

Andres Canella


I was having cellForRowAtIndexPath returning nil and the cause of it for me was that I was calling it in methods where the UITableView must not have been fully initialised and filled. Once I'd moved my call to cellForRowAtIndexPath into -(void)viewDidAppear:(BOOL)animated then my cell was returned correctly.

like image 45
Stephen Watson Avatar answered Oct 21 '22 13:10

Stephen Watson