views:

132

answers:

2

Hi Guys: I have problem with using dequeueReusableCellWithIdentifier method. Whenever I use this one, one cell would display its own value and value that does not belong to it. This first and second images show the problem, while the third image shows the proper data.The problem seems to be caused by old value not being wiped off and the new value being added on top if it. Which seems very strange.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell =nil ; 
if(indexPath.row==0){
    cell = [[[UITableViewCell alloc]init]autorelease];
}else {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


}

NSMutableArray *rowArray = [results objectAtIndex:indexPath.section];
NSString *value=nil;

if (indexPath.section==0) {
    // the row in the 0th section is patient's sticky
    value = [rowArray objectAtIndex:indexPath.row];
    cell.textLabel.text = value;
}else {
    //the row in the 1th section are lab tests
    //here rowArray is tableRecords
    NSArray *rowrecordTemp = [rowArray objectAtIndex:indexPath.row];
    UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 130, tableView.rowHeight-5) ];       
    value = [rowrecordTemp objectAtIndex:0];

    label.text= value;
    [cell.contentView addSubview:label];
    [label release];
    label = nil;


    label =  [[UILabel  alloc] initWithFrame:CGRectMake(140, 0, 50.0, 
                                                        tableView.rowHeight-10)]; 
    value = [rowrecordTemp objectAtIndex:1];
    label.text = value;
    [cell.contentView addSubview:label];
    [label release];
    label = nil;


    label = [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 90, tableView.rowHeight-10)];
    value =  [rowrecordTemp objectAtIndex:2];
    label.text = value;
    [cell.contentView addSubview:label];
    [label release];
    label = nil;


    NSLog(@"%@\t\t%@\t\t\t\t\n%@",[rowrecordTemp objectAtIndex:0] ,[rowrecordTemp objectAtIndex:1],[rowrecordTemp objectAtIndex:2]);


}
NSString *fontname = cell.textLabel.font.fontName;
cell.textLabel.font = [UIFont fontWithName:fontname size:16];
cell.textLabel.numberOfLines = 0;
return cell;

}

+2  A: 

Table view cells are shared in memory. When you dequeue a new one, it will have the value of an old one (I think the last one to be dequeued). You have to clear it of its old contents to display its new contents.

If you're doing something along the lines of cell.textLabel.text = [arr objectAtIndex:indexPath.row] there's no problem because the text will simply be replaced. If you have a custom view or something more complicated, you have to completely replace the view.

eman
Hi eman:I agree with you. And I was doing what u suggested, I thought the old text would be replaced by the assignment. However, seems the old text stays.
Michael Z
Post your `cellForRowAtIndexPath` code--that's probably where the problem is.
eman
A: 

When (section != 0) you're adding new UILabels to the UITableViewCell. Everytime a cell is dequeued, new labels are added to it. You should either remove the old labels, or change the text of the old label instead of creating a new one.

Rengers
Hi Rengers. Thanks for your help. Problem solved as you suggested, i took the second approach to change the text of the old label.
Michael Z