iPhone奇怪的行为UITableView细胞

时间:2011-06-08 22:37:20

标签: iphone ios uitableview detailtextlabel

我有一个包含3个部分的设置视图。某些单元格具有不同的样式:默认或值1。当我快速向上或向下滑动,或更改视图并返回时,应该在单元格中的文本(例如,我的单元格中的detailTextLabel与StyleValue1)不再在此处,或者有时在上方或下方的单元格中。这是截图:第一个是正常状态,第二个是来自Version的detailTextLabel进入上面的单元格,第三个是Measurement System 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 个答案:

答案 0 :(得分:5)

所有单元格都使用相同的reuseIdentifier,因此当您滚动时,您将获得一个旧单元格并在其上设置文本

您可以通过设置解决问题  cell.detailTextLabel.text 对于所有情况

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