TableViewCell没有正确初始化

时间:2015-01-12 21:52:25

标签: objective-c uitableview uitextfield ios8.1

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

当应用程序加载时,默认值为3.其中一个TableViewCells显示另一个ViewController,用户可在其中进行选择。然后使用后退按钮返回到重新加载表的开始VC。

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

任何想法如何阻止这种情况发生?某种重置?

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

谢谢,

1 个答案:

答案 0 :(得分:1)

可能由于使用相同的小区标识符而发生的情况。为每个场景使用两个不同的cellIdentifier(3和4节)。所以它可能看起来像这样:

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

希望这会有所帮助