2

I have a settings view with 3 sections. Some cells have different styles: Default or Value1. When I swipe fast up or down, or change view and come back, the text supposed to be in a cell (for example the detailTextLabel in my cell with StyleValue1) is either not here anymore, or sometimes in a cell above or below... Here is the screenshots: the first is the normal state, the second the detailTextLabel from Version went to the cell above, and in the third the Measurement System detailTextLabel disappeared...

Normal behavior of cells enter image description here enter image description here

And here is my code:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        if (indexPath.section == 1 && indexPath.row == 0) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }
        else if (indexPath.section == 2 && indexPath.row == 2) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }
        else {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    }

    // Selection style.
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    // Vehicles cells.
    if (indexPath.section == 0 && indexPath.row < [self.userCarsArray count]) {
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.text = [[NSString stringWithFormat:@"%@ %@ %@", 
                                [[self.userCarsArray objectAtIndex:indexPath.row] year], 
                                [[self.userCarsArray objectAtIndex:indexPath.row] make], 
                                [[self.userCarsArray objectAtIndex:indexPath.row] model]] uppercaseString];

        // Checkmark if current car.
        if ([[EcoAppAppDelegate userCar] idCar] == [[self.userCarsArray objectAtIndex:indexPath.row] idCar]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            selectedCarPath = indexPath;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    // Add car cell.
    if (indexPath.section == 0 && indexPath.row == [self.userCarsArray count]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.text = @"Add Vehicle";
    }

    // General cells.
    if (indexPath.section == 1 && indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"Measurement System";
        cell.textLabel.textColor = [UIColor darkGrayColor];

        if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)
            cell.detailTextLabel.text = @"Miles";
        else
            cell.detailTextLabel.text = @"Meters";
    }

    // Information cells.
    if (indexPath.section == 2 && indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"About";
        cell.textLabel.textColor = [UIColor darkGrayColor];
    }
    if (indexPath.section == 2 && indexPath.row == 1) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"License";
        cell.textLabel.textColor = [UIColor darkGrayColor];
    }
    if (indexPath.section == 2 && indexPath.row == 2) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = @"Version";
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.detailTextLabel.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return cell;
}

Do you know how I can fix this issue? Thanks!

Dachmt
  • 2,079
  • 4
  • 29
  • 45

1 Answers1

5

All your cells use the same reuseIdentifier, so when you scroll, you get an old cell and you set texts on it

You can solve your problem by setting cell.detailTextLabel.text for all cases

When using reuseIdentifier, you should set every time all fields content that change

AmineG
  • 1,908
  • 2
  • 27
  • 43
  • So either I, all the time, set my cell with everything (selectionStyle, detailTextLabel, the colors, the accessoryType, etc.), or I can just use a different CellIdentifier for each cell right? – Dachmt Jun 08 '11 at 22:56
  • I fixed the problem by setting a unique identifier for each cell using `NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];` Thanks @someone0! – Dachmt Jun 08 '11 at 23:00
  • 1
    the two solutions are possible, but I prefer the first one. The second solution is not recommanded if you have a great number of cells. For better performances, you should create the minimum of cells. – AmineG Jun 08 '11 at 23:04
  • Yeah, I just have one problem, when I add dynamically a cell to my first section (when the user create a new car), it crashes and this because it tries to give the same CellIdentifier I think... I need to find something "really" unique I guess. – Dachmt Jun 08 '11 at 23:32