1

我有分段控件,并且每个分段控件的 onclick 都会显示一个 tableview,它有一个与每个单元格关联的 uistepper。一个段在它的表上有 UIStepper,但是当我在段之间切换时它仍然显示,可能来自其他段。我正在使用 addSubView 方法添加 uistepper。但是当切换到那个段时我如何删除它。

谢谢你

编辑

    if(selectedSegment == 0){
        cell.textLabel.text=[selectedJointWork objectAtIndex:indexPath.row];
        cell.textLabel.font=[UIFont fontWithName:@"Times New Roman" size:17.f];

    }

    if(selectedSegment == 1){
        cell.textLabel.text=[selectedSampling objectAtIndex:indexPath.row];
        cell.textLabel.font=[UIFont fontWithName:@"Times New Roman" size:17.f];

        UILabel *lbl1 = [[UILabel alloc] init];
        lbl1.frame = CGRectMake(400, 16, 35 ,12);
        [lbl1 setBackgroundColor:[UIColor clearColor]];
        //lbl1.text = @"1";
        //lbl1.text = [quantityArray objectAtIndex:indexPath.row];
        [cell.contentView addSubview:lbl1];

        [lbl1 setTag:456];

        UIStepper* stepper = [[UIStepper alloc] init];
        stepper.frame = CGRectMake(450, 10, 100, 10);

        [cell.contentView addSubview: stepper];

        stepper.minimumValue = [[samplingQuantity objectAtIndex:indexPath.row] intValue];

        [stepper setTag:123];
        [stepper addTarget:self action:@selector(stepperOneChanged:) forControlEvents:UIControlEventValueChanged];

        int count = [[samplingQuantity objectAtIndex:indexPath.row] intValue];

        [(UIStepper*)[cell viewWithTag:123] setValue:count];
        [(UILabel*)[cell viewWithTag:456] setText:[NSString stringWithFormat:@"%d",count]];
    }

以下是段切换方法

- (IBAction)segmentSwitch:(id)sender {

UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

[table2 reloadData];

if (selectedSegment == 0) {
    [jointWork removeAllObjects];
    AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.managedObjectContext=appDelegate.managedobjectcontext;

    NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
    [fetch setEntity:[NSEntityDescription entityForName:@"VisitedWith" inManagedObjectContext:self.managedObjectContext]];
    NSArray * result = [self.managedObjectContext executeFetchRequest:fetch error:nil];
    for (NSDictionary *dic in result)
    {
        [jointWork addObject:[dic valueForKey:@"vname"]];
        [Itemsids addObject:[dic valueForKey:@"vid"]];
    }

    if([jointWork count] == 0){
        UIAlertView *save = [[[UIAlertView alloc]
                              initWithTitle:@"Not Found!"
                              message:@"Data not Found"
                              delegate:self
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles: nil] autorelease];
        [save show];
    }

    [table reloadData];

}
else if(selectedSegment == 1){
    [sampling removeAllObjects];
    AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.managedObjectContext=appDelegate.managedobjectcontext;

    NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
    [fetch setEntity:[NSEntityDescription entityForName:@"Sampling" inManagedObjectContext:self.managedObjectContext]];
    NSArray * result = [self.managedObjectContext executeFetchRequest:fetch error:nil];
    for (NSDictionary*dic   in result)
    {
        NSLog(@"Object At index%@",[dic valueForKey:@"productname"]);

        [sampling addObject:[dic valueForKey:@"productname"]];
        [Itemsids addObject:[dic valueForKey:@"productid"]];
    }
    if([sampling count] == 0){
        UIAlertView *save = [[[UIAlertView alloc]
                              initWithTitle:@"Not Found!"
                              message:@"Data not Found"
                              delegate:self
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles: nil] autorelease];
        [save show];
    }

    [table reloadData];
}

}

4

1 回答 1

1

你可以试试这个。

if ([cell.contentView subviews])
{
    for (UIView *subview in [cell.contentView subviews]) 
    {

     if(subview.tag==123 || subview.tag==456)
     [subview removeFromSuperview];

    }
}
于 2013-09-03T13:07:29.247 回答