iPhone странное поведение клеток UITableView - PullRequest
2 голосов
/ 09 июня 2011

У меня есть вид настроек с 3 разделами.Некоторые ячейки имеют разные стили: по умолчанию или значение1.Когда я быстро проведу пальцем вверх или вниз или изменим вид и вернусь, текст, который должен находиться в ячейке (например, detailTextLabel в моей ячейке с StyleValue1), либо больше не находится здесь, либо иногда в ячейке выше или ниже ..Вот скриншоты: первое - это нормальное состояние, второе - detailTextLabel из версии переместилось в ячейку выше, а в третьем исчезла система измерения detailTextLabel ...

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

А вот мой код:

- (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;
}

Знаете ли вы, как я могу решить эту проблему?Спасибо!

1 Ответ

5 голосов
/ 09 июня 2011

Все ваши ячейки используют один и тот же reuseIdentifier, поэтому при прокрутке вы получаете старую ячейку и устанавливаете на нее тексты

Вы можете решить свою проблему, установив cell.detailTextLabel.text для всех случаев

При использовании reuseIdentifier вы должны каждый раз устанавливать содержимое всех полей, которые меняются

...