2

I've created a UITableView with a custom UITableViewCell from a NIB. On loading the view controller, everything seems to work fine. But when I start to scroll the UITableView, there are cases where an empty cell appears in the middle for no reason atall.

I dont know what's happening. My cellForRowAtIndexPath is quite straightforward. But I've added gestures to the custom UITableViewCell. Could that be causing a problem? Here my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *tableIdentifier = @"FTStreamCell";
    FTStreamCell *cell = (FTStreamCell *)[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FTStreamCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.delegate = self;
        return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    FTStreamCell *streamCell = (FTStreamCell*) cell;
    if (!indexPath.section == 0)
    {
        if ((NSNull*)[[[self.cachedData objectForKey:kQuestions] objectAtIndex:indexPath.row] objectForKey:kQuestion] != [NSNull null])
            streamCell.question.text = [[[self.cachedData objectForKey:kQuestions] objectAtIndex:indexPath.row] objectForKey:kQuestion];
        streamCell.timePosted.text = [NSString stringWithFormat:@"Today at Bangalore, India"];
        // To Show My Questions
        NSLog(@"Me %d", [[[NSUserDefaults standardUserDefaults] objectForKey:kUserId] integerValue]);
        if ([[[self.cachedData objectForKey:kQuestions] objectAtIndex:indexPath.row] objectForKey:kUserId] == [[NSUserDefaults standardUserDefaults] objectForKey:kUserId])
        {
            streamCell.mainView.backgroundColor = [UIColor colorWithRed:244.0f/255.0f green:240.0f/255.0f blue:247.0f/255.0f alpha:1];
            streamCell.userCheck = YES;
        }
        else
        {
            streamCell.mainView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1];
            streamCell.userCheck = NO;
        }
        [cell layoutSubviews];
    }
}

Thanks.

etolstoy
  • 1,798
  • 21
  • 33
  • enable NSZombie in Xcode so you can know where its crashing http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode – freelancer Feb 07 '14 at 08:04
  • It doenst give me any crash. But the blank cell is still there. I dont have enough reputation to post a screenshot. Here's the link to it. http://i.stack.imgur.com/AgWfw.png –  Feb 07 '14 at 08:08
  • set breakpoints to to check when is the blank cell loaded. Is it blank because you are using white color in the `else` section? – iphondroid Feb 07 '14 at 08:09
  • i think its crashing becoz you try to access any null object and that why cell being blank. try crashing the app after enabling NSZombie mode than post screen shot here where you got crash. – freelancer Feb 07 '14 at 08:13
  • I havent set it to white anywhere. I've been trying to figure this out for a while now. Since I dont know exactly when it happens, using breakpoints is a lil hard. Keeps happening on random cells while scrolling. –  Feb 07 '14 at 08:13
  • I'm not able to crash the app. That's the issue. Even after enabling NSZombie mode. –  Feb 07 '14 at 08:16
  • That's odd. I enabled NSZombie. I cant seem to reproduce it on my device. But continues to happen on the simulator. –  Feb 07 '14 at 08:22
  • i think your dequeueReusableCellWithIdentifier is the problem – Sunny Shah Feb 07 '14 at 08:24

1 Answers1

0

UITableViews reuse table cells to reduce memory allocation. So when you begin to scroll the 'white cell' is likely a cell that was previously used and modified before. In the willDisplayCell method try 'resetting' the cell before applying your stylistic changes. The 'reset' will be a custom method of yours that changes everything back to their defaults within the cell.

Msencenb
  • 5,675
  • 11
  • 52
  • 84
  • how do I do that? Will assigning it to nil or something help? –  Feb 07 '14 at 09:20
  • You can assign it to nil in the cellForRowAtIndexPath method to force the issue yes. Is there ever a case where section != 0 by the way? – Msencenb Feb 07 '14 at 19:23
  • I have a section with clearColor to show an image behind the tableView. Rest of it is in section 1. Something similar to Path. –  Feb 08 '14 at 04:50