UISwitch里面的自定义UITableViewCell不可用 [英] UISwitch inside custom UITableViewCell not available

查看:138
本文介绍了UISwitch里面的自定义UITableViewCell不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 UITableViewCell 中有一个 UISwitch (我调用的子类 RootLabeledSwitchTableCell )。

I have a UISwitch inside a custom UITableViewCell (the subclass of which I call RootLabeledSwitchTableCell).

该单元格包含 UILabel UISwitch 彼此相邻。

The cell contains a UILabel and UISwitch next to each other.

我有一个名为<$ c的 @property $ c> keychainSwitch 指向此自定义单元格内的开关:

I have a @property called keychainSwitch that points to the switch inside this custom cell:

@interface RootLabeledSwitchTableCell : UITableViewCell {
    IBOutlet UILabel *textLabel;
    IBOutlet UISwitch *labeledSwitch;
}

@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UISwitch *labeledSwitch;

@end

在我的表视图委托中,我添加了一个选择器如果切换开关状态则调用:

In my table view delegate, I have added a selector that is called if the switch state is flipped:

- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSString *CellIdentifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]];
   UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
       switch (indexPath.section) {
       case(kMyFirstSection): {
           switch (indexPath.row) {
               case(kMyFirstSectionFirstRow) {
                   [cellOwner loadMyNibFile:@"RootLabeledSwitchTableCell"];
                   cell = (RootLabeledSwitchTableCell *)cellOwner.cell;
                   self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch];
                   [self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched) forControlEvents:UIControlEventValueChanged];
                   break;
               }
               // ...
           }
       }
       // ...
   }
}

所以这个选择器正常工作:

So this selector works correctly:

- (void) keychainOptionSwitched {
   NSLog(@"Switched keychain option from %d to %d", ![self.keychainSwitch isOn], [self.keychainSwitch isOn]);
}

但是,我不能使用 UISwitch instance的 -setOn:animated:初始化其初始状态的方法:

However, I cannot use the UISwitch instance's -setOn:animated: method to initialize its initial state:

- (void) updateInterfaceState {
   BOOL isFirstTimeRun = [[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue];
   if (isFirstTimeRun) {
      [self.keychainSwitch setOn:NO animated:NO];
   }
}

从测试开始, self。 keychainSwitch nil 这导致 -setOn:animated 什么都不做,但我可以仍然操作开关, NSLog 语句将正确打印开关状态变化,例如:

From testing, self.keychainSwitch is nil which is causing -setOn:animated to do nothing, but I can still operate the switch and the NSLog statement will correctly print the switch state changes, e.g.:

[Session started at 2009-08-24 07:04:56 -0700.]
2009-08-24 07:04:58.489 MyApp[29868:20b] keychain switch is: nil
2009-08-24 07:05:00.641 MyApp[29868:20b] Switched keychain option from 1 to 0
2009-08-24 07:05:01.536 MyApp[29868:20b] Switched keychain option from 0 to 1
2009-08-24 07:05:02.928 MyApp[29868:20b] Switched keychain option from 1 to 0

UITableView 委托方法中设置 self.keychainSwitch 是否遗漏了什么? ?

Is there something I am missing about setting self.keychainSwitch in the UITableView delegate method?

推荐答案

我刚刚做过这个。
您应该更改您的selctor

I did this a while ago. You should change your selctor

[self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched:) forControlEvents:UIControlEventValueChanged];

然后将您的方法更新为

-(void) keychainOptionSwitched:(id)sender {
UISwitch *tempSwitch = (UISwitch *)sender;
}

现在tempSwitch是已更改的开关。

Now tempSwitch is the switch that was changed.

这篇关于UISwitch里面的自定义UITableViewCell不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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