how to check status of multiple UISwitch controls in a table view with custom cell using tag property

戏子无情 提交于 2019-12-11 07:57:13

问题


I have a tableview with custom UITableViewCell. Cell have a UISwitch controls. I have added cell into the table view controller with the same action for all switches. I have added tag value of UISwitch in cellForRowAtIndexPath method.

I want to determine what switch was changed, when user changed the switch status on/off. Here I am setting action on UISwitch button.

- (void)viewDidLoad
{
    [super viewDidLoad];
    cell.switchbtn.userInteractionEnabled=YES;
    [cell.switchbtn setOn:YES];
    [cell.switchbtn addTarget:self action:@selector(switchToggled:) forControlEvents:   UIControlEventValueChanged];
    [cell.contentView addSubview:cell.switchbtn];
}

Here I am setting tag value of uiswitch

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

    if (!cell)
    {
        [[NSBundle mainBundle] loadNibNamed:@"cellid" owner:self options:nil];

    }

    cell.switchbtn.tag=indexPath.row;
    NSLog(@"btn tag=%d",cell.switchbtn.tag);
    return cell;
}

Here I am calling switchToggled: method to get the uiswitch status.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
  [self switchToggled:cell.switchbtn];
}

I am gettting tag value and each time status is On .even i status is off.

- (void) switchToggled:(UISwitch *)sender {
    UISwitch *mySwitch = (UISwitch *)sender;

    NSLog(@"tag ==%@",mySwitch);
    if ([mySwitch isOn]) {
        NSLog(@"its on!");
    } else {
        NSLog(@"its off!");
    }
}

回答1:


Use CGpoint with direct Cell's property to identify which row is selected and that cell's Uiswitch's state you can identify easily even more accurate and this is the one method am using and it is working fine for me.

- (void)switchToggled:(id)sender
{

    CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableName]];
    NSIndexPath *indexPath = [[self tableName] indexPathForRowAtPoint:switchPositionPoint];

    BusinessHoursCell *cell = (BusinessHoursCell*)[self.tableName cellForRowAtIndexPath:indexPath];

    int tag=indexPath.row;

    if (tag==0)
    {
        if (cell.workingDaySwitch.on)
        {
            NSLog(@"its on!");
        }

        else
        {
            NSLog(@"its off!");
        }
    }
}



回答2:


I'd use the following scheme: In cellForRowAtIndexPath:.. set current indexPath to cell. Create protocol with method like - (void)cellAtIndexPath:(NSIndexPath *)path changedSwitchStateToState:(BOOL)state make controller delegate for cells. Cell handles switch state change by calling that delegate method.

In controller you should use something for dataSource(probably custom class) which stores objects which are used to configure the cell and has a method similar to - (void)changeSwitchStateToState:(BOOL)state atIndexPath:(NSIndexPath *)path where it gets corresponding object and updates state.



来源:https://stackoverflow.com/questions/25142567/how-to-check-status-of-multiple-uiswitch-controls-in-a-table-view-with-custom-ce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!