UITableViewCell 未正确初始化

TableViewCell not initializing correctly

我在 ViewController 中使用 TableView。有两种情况,一种有 3 个部分,一种有 4 个部分。

当应用程序加载时,默认值为 3。其中一个 TableViewCells 显示另一个 ViewController,用户可以在其中做出选择。然后使用后退按钮 return 到 VC 重新加载 table 的位置。

当用户从 4 个部分返回到 3 个部分时,问题就出现了。正如您在下面的代码中看到的那样,第 3 部分(案例 2)在显示 4 个部分时实现了一个 textField。当用户移回 3 时,textField 仍然存在。我假设 table 会在重新加载时从头开始。相反,它会保留以前的设置并在顶部添加新设置。

有什么想法可以阻止这种情况发生吗?某种重置?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

{
    UITableViewCell *serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell"];



        if (sections == 3) {
            switch (indexPath.section) {
                case 0:
                    if ([testLocation isEqualToString:@""]) {
                        serverLocCell.textLabel.text = @"Choose location";
                    }
                    else {
                        serverLocCell.textLabel.text = testLocation;
                    }

                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

                    break;
                case 1:
                    serverLocCell.textLabel.text = testType;
                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case 2:
                    serverLocCell.textLabel.text = @"Start Test";
                    serverLocCell.textLabel.textColor = [UIColor whiteColor];
                    serverLocCell.textLabel.textAlignment = NSTextAlignmentCenter;
                    serverLocCell.detailTextLabel.text = @"";
                    serverLocCell.accessoryType = UITableViewCellAccessoryNone;
                    serverLocCell.backgroundColor = [UIColor colorWithRed:0.0 / 255 green:102.0 / 255 blue:51.0 / 255 alpha:1.0];
                    break;
                default:
                    break;
            }
        }
        else {
            switch (indexPath.section) {
                case 0:
                    if ([testLocation isEqualToString:@""]) {
                        serverLocCell.textLabel.text = @"Choose location";
                    }
                    else {
                        serverLocCell.textLabel.text = testLocation;
                    }

                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

                    break;
                case 1:
                    serverLocCell.textLabel.text = testType;
                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case 2:
                    if ([testType isEqualToString:@"Published App Test"]) {

                        publishedAppTextField = [[UITextField alloc]init];
                        publishedAppTextField.tag = 1;
                        [publishedAppTextField addTarget:self
                                                  action:@selector(textFieldDidChange:)
                                        forControlEvents:UIControlEventEditingChanged];

                        serverLocCell.textLabel.text = @"";
                        serverLocCell.accessoryType = UITableViewCellAccessoryNone;
                        publishedAppTextField.frame = CGRectMake(7, 10, 300, 30);
                        publishedAppTextField.clearsOnBeginEditing = YES;
                        publishedAppTextField.delegate = self;
                        publishedAppTextField.returnKeyType = UIReturnKeyDone;
                        [serverLocCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        [serverLocCell.contentView addSubview:publishedAppTextField];
                        serverLocCell.backgroundColor = [UIColor whiteColor];
                        break;
                    }
                    else {

                        break;
                    }
                case 3:
                    serverLocCell.textLabel.text = @"Start Test";
                    serverLocCell.textLabel.textColor = [UIColor whiteColor];
                    serverLocCell.textLabel.textAlignment = NSTextAlignmentCenter;
                    serverLocCell.detailTextLabel.text = @"";
                    serverLocCell.accessoryType = UITableViewCellAccessoryNone;
                    serverLocCell.backgroundColor = [UIColor colorWithRed:0.0 / 255 green:102.0 / 255 blue:51.0 / 255 alpha:1.0];
                    break;
                default:
                    break;
            }

        }
    return serverLocCell;
}

谢谢,

发生这种情况的原因可能是使用了相同的小区标识符。为您拥有的每个场景(3 和 4 部分)使用两个不同的 cellIdentifiers。所以它可能看起来像这样:

if (sections == 3) {
    serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell1"];
    ...
} else if (sections == 4) {
    serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell2"];
    ...
}

希望这对您有所帮助