Reusing Cells in Table View Causing Problems

Having issues with Cell setup in a TableView...I set up table view with an image view, and two labels, one title, and one details. First part looks fine, but after scrolling, it continually piles all the labels on top of each other.

Here is my code:

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

}



RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
   NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
NSLog(@"%@", articleDateString);
//  cell.textLabel.text = entry.articleTitle; 
// cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];

UIFont *cellFont = [UIFont fontWithName:@"ArialRoundedMTBold" size:17];    
//cell.textLabel.font = cellFont;
UIFont *cellFont2 = [UIFont fontWithName:@"ArialRoundedMTBold" size:12];    
//cell.detailTextLabel.font = cellFont2;
UIImage *img = [UIImage imageNamed:@"anicon.png"];
UIImageView *alternate = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,70,70)];


alternate.image = img;
UILabel *alternatelabel = [[UILabel alloc] initWithFrame:CGRectMake(75,0,210,70)];
alternatelabel.backgroundColor = [UIColor clearColor];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 20, 225, 70)];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];
detailLabel.font = cellFont2;
alternatelabel.font = cellFont;

alternatelabel.text = entry.articleTitle;

[cell.contentView addSubview:alternate];
[cell.contentView addSubview:alternatelabel];
[cell.contentView addSubview:detailLabel];
[detailLabel release];
[alternatelabel release];
[alternate release];
NSLog(@"imageurl%@", img);

return cell;
}

Answers


i would like to suggest you that just alloc and add into cell sub component inside this method for cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 20, 225, 70)];
detailLabel.tag = 88;
detailLabel.backgroundColor = [UIColor clearColor];


[cell.contentView addSubview:detailLabel];
}

Then write this line out side if loop.

UILabel *detailLabel = (UILabel *)[cell viewWithtag:88];
detailLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];

don't forgot to assign tag value.

then out you can access like bellow

Hope you will got what i am trying to say....


Thats because you keep adding the labels on reused cells, when a cell is reused it comes back as it was, (with the previous label you added), and you keep adding the labels...

Solutions..

1- Subclass UITableViewCell to have the labels you want and just set their text and properties

2- Remove the labels from the cells (if they have them) and re add the cells, or mark them with tags so you can find them later and set their text (i would probably go for option 1)


http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

Best tut for your problem


Need Your Help