【问题标题】:DCRoundSwitch In Custom Cell Fires On Table Reload自定义单元格中的 DCRoundSwitch 在表重新加载时触发
【发布时间】:2012-09-24 14:58:24
【问题描述】:

我有一个包含四行的 UITableView。每行都有一个带有 DCRoundSwitch 的自定义单元格。 value-changed 事件在 IB 中链接到 switchChanged 方法。每次在 cellForRowAtIndexPath 中需要一个带有开关的新单元时,我都会将新单元的开关分配给 ivar,这样我就可以像这样以编程方式控制它:

       self.categorySwitch = ((FilterViewCell *)cell).sectionSwitch; 

在我第一次触摸开关后,它完美运行,触发了 switchChanged 方法。

问题: 每次需要一个新单元格或重新加载表时,它都会触发switchedChanged 方法,而无需触摸开关。我已经尝试像这样在 switchChanged 方法的末尾删除所有目标,但它仍然不起作用:

       [theSwitch removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];

使用 UISwitch 可以完美运行。

有什么建议吗? DCRoundSwitch 似乎是一个很好的选择,我想使用它。

谢谢

【问题讨论】:

    标签: uitableview uiswitch


    【解决方案1】:

    尝试将事件更改为 UIControlEventTouchUpInside,如下所示...

       [theSwitch removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
    

    同时在 DCRoundSwitch.m 文件中更改 setOn:animated: 方法中的事件

       [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    

    【讨论】:

      【解决方案2】:

      在 DCRoundSwitch.m 文件中,定位方法:

      - (void)setOn:(BOOL)newOn animated:(BOOL)animated ignoreControlEvents:(BOOL)ignoreControlEvents
      

      删除这两行:

      [[self allTargets] makeObjectsPerformSelector:@selector(retain)];
      [[self allTargets] makeObjectsPerformSelector:@selector(release)];
      

      【讨论】:

      • 您能否在您的代码中添加一些 cmets 以便给出完整的答案
      猜你喜欢
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多