如何使用标签属性检查带有自定义单元格的表格视图中多个 UISwitch 控件的状态 [英] how to check status of multiple UISwitch controls in a table view with custom cell using tag property

查看:28
本文介绍了如何使用标签属性检查带有自定义单元格的表格视图中多个 UISwitch 控件的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义 UITableViewCell 的 tableview.单元格有一个 UISwitch 控件.我已将单元格添加到表视图控制器中,所有开关都具有相同的操作.我在 cellForRowAtIndexPath 方法中添加了 UISwitch 的标签值.

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.

当用户更改开关状态开/关时,我想确定更改了哪个开关.在这里,我在 UISwitch 按钮上设置操作.

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

这里我设置了uiswitch的标签值

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

这里我调用 switchToggled: 方法来获取 uiswitch 状态.

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

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

我正在获取标签值,每次状态为 On .even i status 为 off.

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!");
    }
}

推荐答案

使用具有直接 Cell 属性的 CGpoint 来识别选中了哪一行以及该单元格的 Uiswitch 状态,您可以轻松更准确地识别,这是我正在使用的一种方法,它对我来说很好.

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!");
        }
    }
}

这篇关于如何使用标签属性检查带有自定义单元格的表格视图中多个 UISwitch 控件的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆