0

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

在每个单元格中,我都添加了一个 textField 子视图。因此,在第 0 段,我将有三行包含邮政编码、半径和 API 密钥。对于第 1 段,我将有四行包含纬度、经度、半径和 API 密钥。但是,当我来回切换时,没有正确删除 textField 子视图。(Radius 和 API Key 字段是相同的字段,但切换时它们会显示在不同的行上。)

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

请查看我的代码,我将不胜感激任何指针,因为我不确定在哪里以及如何最好地删除这些子视图。

在此处输入图像描述 在此处输入图像描述

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

}
4

1 回答 1

0

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

如:

if ([mySegmentControl selectedSegmentIndex] == 0) {

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

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

于 2012-12-17T21:13:55.170 回答