2

我有一个包含 3 个部分的设置视图。某些单元格具有不同的样式:默认或 Value1。当我快速向上或向下滑动,或更改视图并返回时,应该在单元格中的文本(例如,我的单元格中带有 StyleValue1 的 detailTextLabel)要么不再存在,要么有时位于上方或下方的单元格中。 .这里是截图:第一个是正常状态,第二个是来自Version的detailTextLabel到上面的单元格,第三个是Measurement System detailTextLabel消失了......

细胞的正常行为 在此处输入图像描述 在此处输入图像描述

这是我的代码:

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

你知道我该如何解决这个问题吗?谢谢!

4

1 回答 1

5

您所有的单元格都使用相同的重用标识符,因此当您滚动时,您会得到一个旧单元格并在其上设置文本

您可以通过为所有情况设置 cell.detailTextLabel.text 来解决您的问题

使用reuseIdentifier时,应设置每次更改的所有字段内容

于 2011-06-08T22:43:27.740 回答