分段控制中TableView中的UIStepper

时间:2013-09-03 12:37:34

标签: ios uitableview uistepper uisegmentedcontrol

我对每个分段控件进行了分段控制和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];
}

}

1 个答案:

答案 0 :(得分:1)

你可以试试这个。

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

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

    }
}