10

私は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

4

3 に答える 3

70

UISwitchをアクセサリビューとして追加できることに気付く前に、シンプルなラベルが付いたカスタムUITableViewCellsをいじって、スイッチをオンにして何年も過ごしました。あなたはおそらくこれを知っていて、他の理由でカスタムセルが必要ですが、念のためにこれについて言及したいと思います!

これは次のように行います(-tableView:cellForRowAtIndexPathメソッドで)。

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

AccessoriesViewビットは、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

ここで起こっているのは、セルが表示されていないときに操作を呼び出そうとしていることだと思います。そのため、セルはその時点でアンロードされているため、キーチェーンスイッチが入るとゼロになります。表示すると、リロードされてからnilではなくなります。しかし、私はあなたがそのようにスイッチを割り当てているのを見ます

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

これはテーブルセル内のスイッチへの参照であり、保持していません。そのため、セルが非表示になり、結果としてkeychainSwitchがnilになり、クラスがnilになるため、スイッチがnilになります。メンバーもゼロになりつつあります。keychainSwitchを保持してから、テーブルを再構築するときに、keychainSwitchなどからスイッチを取り出します。お役に立てれば

于 2009-08-24T14:57:24.857 に答える