切换从tableView单元格中删除/添加子视图

时间:2012-12-17 19:35:57

标签: ios uitableview

我的tableView有一个带有分段控件的标题。段0将显示我的tableView有3行,段1将显示4行。

在每个单元格中,我添加了一个textField子视图。所以在零段我将有三行包含Zip Code,Radius和API Key。对于段1,我将有四行带有纬度,经度,半径和API密钥。但是,当我来回切换时,textField子视图未被正确删除。 (Radius和API Key字段是相同的字段,但在切换时它们将显示在不同的行上。)

我尝试过标记textFields然后删除带有该标记的textField,但这种情况不稳定。我已经测试了textField的存在,如果它存在,请在切换时将其删除。 (这在下面的示例代码中。) 我也尝试过使用segmentChanged方法无济于事。

请查看我的代码,我很感激任何指示,因为我不确定删除这些子视图的位置和方式。

enter image description here enter image description here

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if ([mySegmentControl selectedSegmentIndex] == 0) {
            return 3;
        } else {
            return 4;
        }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if ([mySegmentControl selectedSegmentIndex] == 0) {

        if (latField) {
            [latField removeFromSuperview];
            [longField removeFromSuperview];
            [radiusField removeFromSuperview];
            [apiKeyField removeFromSuperview];

        }

        if ([indexPath row] == 0 && [indexPath section] == 0) {

            zipCodeField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];   
            [[cell textLabel] setText:@"Zip Code:"];
            [cell.contentView addSubview:zipCodeField];


        } else if ([indexPath row] == 1 && [indexPath section] == 0) {

            radiusField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];            
            [[cell textLabel] setText:@"Radius:"];
            [cell.contentView addSubview:radiusField];

        } else if ([indexPath row] ==2 && [indexPath section] == 0) {

            apiKeyField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 145, 30)];
            [[cell textLabel] setText:@"API Key:"];
            [cell.contentView addSubview:apiKeyField];

    }

    else if ([mySegmentControl selectedSegmentIndex] == 1) {

        if (zipCodeField) {
            [zipCodeField removeFromSuperview];
            [radiusField removeFromSuperview];
            [apiKeyField removeFromSuperview];
        }

        if ([indexPath row] == 0 && [indexPath section] == 0) {

            latField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
            [[cell textLabel] setText:@"Latitude:"];
            [cell.contentView addSubview:latField];

        } else if ([indexPath row] ==1 && [indexPath section] == 0) {

            longField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
            [[cell textLabel] setText:@"Longitude:"];
            [cell.contentView addSubview:longField];

        } else if ([indexPath row] ==2 && [indexPath section] == 0) {

            radiusField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
            [[cell textLabel] setText:@"Radius:"];
            [cell.contentView addSubview:radiusField];

        } else if ([indexPath row] == 3 && [indexPath section] == 0) {

            apiKeyField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 145, 30)];          
            [[cell textLabel] setText:@"API Key:"];
            [cell.contentView addSubview:apiKeyField];
        }

    }
    return cell;
}


- (IBAction)segmentChanged:(id)sender {

    [storeFinderTableView reloadData];

}

1 个答案:

答案 0 :(得分:0)

没关系。我没有尝试重用我正在添加到tableView单元格中的textFields,而是为每个单元格创建了唯一的textField,然后根据段控件测试并删除它们。

如:

if ([mySegmentControl selectedSegmentIndex] == 0) {

        if (latField) {
            [latField removeFromSuperview];
            [longField removeFromSuperview];
            [latRadiusField removeFromSuperview];
            [latApiKeyField removeFromSuperview];
        }

然后将textFields添加到单元格的子视图中,反之亦然,将段落== 1。