私はUISwitch
習慣の中にありますUITableViewCell
(私が呼ぶサブクラスRootLabeledSwitchTableCell
)。
セルには、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
テーブルビューデリゲートに、スイッチの状態が反転した場合に呼び出されるセレクターを追加しました。
- (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.keychainSwitch
は何も起こらないnil
原因-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.keychainSwitch
の設定に欠けているものはありますか?UITableView