Question

I have segmented control and onclick of each segmented control a tableview is shown which has a uistepper associated with each cell. One segment dosent have UIStepper on its tables but still it shows up when i switch between segments, possibly from other segments. I am using addSubView method to add the uistepper. but how do i remove it when switched to that one segment.

Thank You

EDIT

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

Following is Segment Switch Method

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

}

Was it helpful?

Solution

You can try this.

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

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

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top