Search code examples
iphoneiosuitableviewdetailtextlabel

iPhone Weird behavior UITableView cells


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!


Solution

  • 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