10

我有一个UISwitch内部自定义UITableViewCell(我称之为的子类RootLabeledSwitchTableCell)。

单元格包含 aUILabelUISwitch彼此相邻。

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

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

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

@end

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

- (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;
               }
               // ...
           }
       }
       // ...
   }
}

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

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

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

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

从测试来看,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

self.keychainSwitchUITableView委托方法中设置我有什么遗漏吗?

4

3 回答 3

70

我花了很长时间摆弄带有简单标签的自定义 UITableViewCells 并打开它,然后才发现我可以添加一个 UISwitch 作为附件视图。无论如何,您可能已经意识到这一点,并且出于其他原因想要自定义单元格,但以防万一我想提一下!

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

UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;

accessoryView 位还负责调整大小和定位,因此我们可以摆脱 CGRectZero。

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

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

希望有帮助!

于 2009-08-24T15:27:16.540 回答
5

我不久前做过这个。你应该改变你的选择器

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

然后将您的方法更新为

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

现在 tempSwitch 是已更改的开关。

于 2009-08-28T19:53:38.967 回答
0

我认为这里发生的事情是您试图在单元格不在视野范围内时调用该操作,所以发生的情况是单元格当时已卸载,因此一旦钥匙串开关进入,您将获得零查看然后它不再为零,因为它重新加载。但是我看到您正在像这样分配开关

 self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch];

它是对表格单元格中开关的引用,您没有保留它,因此它使开关变为 nil ,因为当单元格消失时被卸载并且 keychainSwitch 结果变为 nil ,因此您的班级member 也变为零了。您可能希望保留 keychainSwitch,然后在重建表单元时将开关从 keychainSwitch 或类似的东西中取出。希望这可以帮助

于 2009-08-24T14:57:24.857 回答