views:

41

answers:

1

I am not sure why my UITableViewCell's are repeating after the user scrolls?

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

     ChartlyCell *cell = (ChartlyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
      //cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier andDelegate:self] autorelease];
     }


     ChartlyObject *myChartlyObject;
     myChartlyObject = [self.items objectAtIndex:indexPath.row];

     cell.usernameLabel.text = myChartlyObject.userName;
     cell.bodyLabel.text = myChartlyObject.chart_message;
     [cell.messageLabel setText: myChartlyObject.chart_message];
     cell.timeLabel.text = [Utils toShortTimeIntervalStringFromStockTwits: myChartlyObject.created_at];
     [cell.chartImage loadImageFromURL:[NSURL URLWithString:myChartlyObject.imageThumbnail]];  

        return cell;
    }

This is how the cell is being created:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andDelegate:(id<NLabelDelegate>)ndelegate{
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
  CGRect rect;

  rect = CGRectMake(77, 5, 200, 20); //CGRectMake(10, 5, 200, 20);
  usernameLabel = [[UILabel alloc] initWithFrame:rect];
  //usernameLabel.backgroundColor = backgroundColor;
  usernameLabel.font = [Utils getBoldSystemFontWithSize:14];
  usernameLabel.textColor = [UIColor colorWithRed:.368 green:.741 blue:.784 alpha:1];
  [self.contentView addSubview: usernameLabel];

  rect = CGRectMake(277, 5, 38, 20);
  timeLabel = [[UILabel alloc] initWithFrame:rect];
  timeLabel.textAlignment = UITextAlignmentRight;
  //timeLabel.backgroundColor = backgroundColor;
  timeLabel.font = [Utils getBoldSystemFontWithSize:14];
  timeLabel.textColor = [UIColor colorWithRed:.98 green:.65 blue:.01 alpha:1];
  [self.contentView addSubview: timeLabel];

  /*rect = CGRectMake(77, 25, 238, 68); //CGRectMake(10, 50, 238, 68);
  bodyLabel = [[UILabel alloc] initWithFrame:rect];
  bodyLabel.font = [Utils getSystemFontWithSize:10];
  bodyLabel.numberOfLines = 3;
  bodyLabel.lineBreakMode = UILineBreakModeWordWrap;
  bodyLabel.textColor = [UIColor blackColor];
  bodyLabel.backgroundColor = [UIColor clearColor];
  [self.contentView addSubview: bodyLabel];*/

  rect = CGRectMake(77, 25, 238, 68);
  messageLabel = [[NLabel alloc] initWithFrame:rect andDelegate:ndelegate];
  [messageLabel setOpaque:NO];
  [messageLabel setBackgroundColor: [UIColor blackColor]];
  //[messageLabel setTotalheight:68];
  [self.contentView addSubview: messageLabel];

  rect = CGRectMake(13, 10, 48, 48);
  chartImage = [[AsyncImageView alloc]initWithFrame:rect];
  [self.contentView addSubview: chartImage];
    }
    return self;
}

UPDATE: What is odd, is that if I uncomment the bodyLabel block and comment out the messageLabel block the repeating cells don't appear anymore. I am not sure why and would still like to find a resolution

UPDATE #2 Only messageLabel gets repeated. All of the other labels do NOT repeat. Why is this?

+1  A: 

Because you are re-using tablecells for proper memory management as you should be doing via dequeueReusableCellWithIdentifier :) This way you don't have a unique cell for every single cell you need. Instead you only have about 5 or 6 that get re-used over and over during scrolling.

When the cell comes back into view, you need to reset the state (i.e. update the info, labels, images, etc) of each cell based on whatever cell came into view.

iWasRobbed
can you show an example of where I need to reset the state?
Sheehan Alam
im under the impression I am updating the cell, after that if block
Sheehan Alam
You may want to look at how apple does this since it really depends on how you have create the cell either via `drawRect` or with a XIB. http://developer.apple.com/iphone/library/samplecode/AdvancedTableViewCells/Introduction/Intro.html
iWasRobbed
I think I did follow their steps. They create a cell if nil, and then set the data properties like I do afterwards.
Sheehan Alam
Right, but did you set it up the same as they did as far as in their `Composite` or `Individual` cell docs? They are actually setting the data in those files. I'm assuming you're creating it with a XIB file? If so, you'd do it similarly to their `Individual` cell implementation file.
iWasRobbed
I am creating it from scratch. I have updated my question, with the initialization code I'm using to build the cell.
Sheehan Alam
What is odd, is that the re-use scrolling problem goes away if i comment out the messageLabel block, and uncomment the bodyLabel block.
Sheehan Alam
Could it be something to do with the delegation of that label? The other labels are standard UILabels and they seem to be updating fine.
iWasRobbed
Maybe the delegate is not working properly so the label never gets updated.
iWasRobbed
good reasoning, I believe the label is getting updated because the text does appear, just not on scroll?
Sheehan Alam
you mean it appears after you stop scrolling (like it waits for the UIView to update itself and then it shows)? also, what does your `setText` method look like for the `messageLabel`? you might wanna set an NSLog in that method to see when it gets called and see how that relates to the view or during scrolling. it should get called just as the cell is coming onto the screen... if it's being delayed, there might be something else going on here
iWasRobbed
I need to assign the delegate if a cell is not nil as well. Seemed to be the culprit! Thx for your persistence.
Sheehan Alam
Glad you figured it out!
iWasRobbed