UISwitch里面的自定义UITableViewCell不可用

Ale*_*lds 10 iphone properties uitableview uiswitch

我有一个UISwitch自定义内部UITableViewCell(我调用的子类RootLabeledSwitchTableCell).

单元格包含a UILabel并且UISwitch彼此相邻.

我有一个@property调用keychainSwitch指向此自定义单元格内的开关:

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

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

@end
Run Code Online (Sandbox Code Playgroud)

在我的表视图委托中,我添加了一个在翻转开关状态时调用的选择器:

- (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;
               }
               // ...
           }
       }
       // ...
   }
}
Run Code Online (Sandbox Code Playgroud)

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

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

但是,我无法使用UISwitch实例的-setOn:animated:方法初始化其初始状态:

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

从测试的,self.keychainSwitchnil它造成-setOn:animated什么也不做,但我仍然可以操作开关和NSLog语句将正确打印开关状态的变化,例如:

[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
Run Code Online (Sandbox Code Playgroud)

self.keychainSwitchUITableView委托方法中设置是否有一些我缺少的东西?

h4x*_*xxr 70

我花了很多时间摆弄着自定义的UITableViewCells,带有一个简单的标签,并在我发现之前切换它,我可以添加一个UISwitch作为附件视图.无论如何你可能都知道这个,并且出于其他原因想要自定义单元格,但万一我想提一下!

您可以按如下方式执行此操作(在-tableView:cellForRowAtIndexPath方法中):

UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
Run Code Online (Sandbox Code Playgroud)

accessoryView位还可以处理尺寸和位置,因此我们可以使用CGRectZero.

然后,您可以使用系统控件事件和setOn方法,如下所示(注意我们将其转换为我们知道的UISwitch指针):

[(UISwitch *)cell.accessoryView setOn:YES];   // Or NO, obviously!
[(UISwitch *)cell.accessoryView addTarget:self action:@selector(mySelector)
     forControlEvents:UIControlEventValueChanged];
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!

  • [cell addSubview:mySwitch]; 没有必要.cell.accessoryView保留mySwitch. (11认同)

小智 5

我刚才这样做了.你应该改变你的选择

[self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched:) forControlEvents:UIControlEventValueChanged];
Run Code Online (Sandbox Code Playgroud)

然后将您的方法更新为

-(void) keychainOptionSwitched:(id)sender {
UISwitch *tempSwitch = (UISwitch *)sender;
}
Run Code Online (Sandbox Code Playgroud)

现在tempSwitch是改变的开关.