简体   繁体   中英

UITableViewCell Added UISwitch on change show UIbutton on cell

I want to Show/Hide UIButton as per the state of UISwitch on/off. While scrolling tableview UIButton gets change state.. It display on other cell also. Please advise. In UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    EnhancedChecklistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if ( cell == nil )
    {
        NSArray *topLevelObjects;
        if(IS_IPAD)
        {
            topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EnhancedChecklistTableViewCell" owner:self options:nil];

        }
        else
        {
            topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EnhancedChecklistTableViewCell_Iphone" owner:self options:nil];

        }
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[EnhancedChecklistTableViewCell class]])
            {
                cell = (EnhancedChecklistTableViewCell *)currentObject;
                cell.delegate = self;
                break;
            }
        }
    }

    R5OPERATORACTCHECKLISTS *inspectionTasks = [_data objectAtIndex:indexPath.section];
    return cell;
}

all the Switches are added using xib file in custom cell. I have used one method which will fire after value change event. and as per the state of the switch I am displaying button. But while scrolling it disturb.

create .h file bool variable name "switchFlag" and default set YES and after that you can check in cellForRowAtIndexPath method like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
    }
    if (switchFlag ==YES)
    {
        NSLog(@"Switch On");
    }
    else
    {
        NSLog(@"Switch Off");
    }
}

and when you click on UISwitch that time change Value switchFlag set YES Or NO depends on UISwitch State, so it's giving you expected output , i hope it will help you.

It should be like this

First you can take NSMutableArray with switches default state is On/Off (1/0).

self.switchStateArray = [[NSMutableArray alloc]initWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1", nil];

And in cellForRowAtIndexPath you can call the below method on switch action

cell.normalSwitch.tag = indexPath.row;

[cell.normalSwitch addTarget:self action:@selector(switchToggle:) forControlEvents:UIControlEventValueChanged];

 cell.normalSwitch.userInteractionEnabled = YES;

if (cell.normalSwitch.isOn)
{
    cell.buttonName.hidden = NO;

    NSLog(@"Switch is On");
}
else
{
    cell.buttonName.hidden = YES;

   NSLog(@"Switch is Off");

}

//Switch Action

-(void)switchToggle:(id)sender
{
    UISwitch *theSwitch = (UISwitch *)sender;

    NSLog(@"tag %ld",(long)theSwitch.tag);
    NSInteger index = [sender tag];

    if (theSwitch.isOn)
    {
        [self.switchStateArray replaceObjectAtIndex:index withObject:@"1"];
        [theSwitch setOn:YES animated:YES];
    }
    else
    {
        [self.switchStateArray replaceObjectAtIndex:index withObject:@"0"];
        [theSwitch setOn:NO animated:YES];
    }

    [self.tableviewName reloadData];


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM