在 Prototype Cell 中访问 UISwitch [英] Access UISwitch in Prototype Cell

查看:22
本文介绍了在 Prototype Cell 中访问 UISwitch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户更改我的 UISwitch 的状态时,我正在尝试打开/关闭打印.

I'm trying to print on/off when the user changes the state of my UISwitch.

例如

- (IBAction)toggleSwitch:(id)sender {

    ChannelsTableViewCell* cell = (ChannelsTableViewCell *)[sender superview].superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    if (cell.localSwitch.on)
    {
        NSLog(@"On");
    }

}

当我运行它时它会抛出一个错误.

When I run this it throws an error.

2014-04-24 11:33:41.462 HRApp[3258:60b] -[UITableViewCellScrollView localTitle]:无法识别的选择器发送到实例 0x193544102014-04-24 11:33:41.467 HRApp[3258:60b] *** 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[UITableViewCellScrollView localTitle]:无法识别的选择器发送到实例 0x19354410"

2014-04-24 11:33:41.462 HRApp[3258:60b] -[UITableViewCellScrollView localTitle]: unrecognized selector sent to instance 0x19354410 2014-04-24 11:33:41.467 HRApp[3258:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellScrollView localTitle]: unrecognized selector sent to instance 0x19354410'

推荐答案

如果你把 IBAction 挂接到 switch 上,你就不需要得到 UITableViewCell检查开关的值.您可以使用 IBAction 中的 sender 参数.因此:

If you hook up the IBAction to the switch, you don't need to get the UITableViewCell to check for the switch's value. You can use the sender parameter from the IBAction. Hence:

- (IBAction)toggleSwitch:(id)sender {
    UISwitch *switch = (UISwitch *)sender;
    if (switch.on)
    {
        NSLog(@"On");
    }

}

如果您需要找到显示UISwitchindexPath,您可以添加以下内容:

If you need to find the indexPath at which the UISwitch is shown, you can add the following:

CGPoint pointInTable = [switch convertPoint:switch.bounds.origin toView:self.tableView];    
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTable];

这篇关于在 Prototype Cell 中访问 UISwitch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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