【问题标题】:Toggle removal/addition of subviews from tableView cell从 tableView 单元格中切换删除/添加子视图
【发布时间】:2012-12-18 05:13:55
【问题描述】:

我的 tableView 有一个带有分段控件的标题。 Segment 0 将显示我的 tableView 3 行,segment 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];

}

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

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

    如:

    if ([mySegmentControl selectedSegmentIndex] == 0) {
    
            if (latField) {
                [latField removeFromSuperview];
                [longField removeFromSuperview];
                [latRadiusField removeFromSuperview];
                [latApiKeyField removeFromSuperview];
            }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多