質問

私は、カスタムUISwitch(私はUITableViewCell呼んそのサブクラス)の内部RootLabeledSwitchTableCellを持っています。

セルが互いに隣接UILabelUISwitchが含まれます。

私はこのカスタムセル内のスイッチを指す@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の設定について行方不明です何かがありますか?

役に立ちましたか?

解決

私はしばらく前にこれをしませんでした。 あなたのselctorを変更する必要があります。

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

そしてへのあなたの方法を更新

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

今tempSwitchが変更されたスイッチです。

他のヒント

私はシンプルなラベルでカスタムUITableViewCellsの周りいじる年代を過ごし、私はちょうどは、アクセサリビューのようUISwitchを追加することができます発見する前にそれに切り替えます。とにかく、おそらくこのことを認識していると、他の理由のためにカスタムセルをしたいが、念のために私はこれを言及したかった!

あなたが(:cellForRowAtIndexPathメソッド-tableViewで):次のようにこれを行う

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

accessoryViewビットもサイズと位置の世話をするので、我々はCGRectZeroで逃げることができます。

あなたは(我々はそれを知っているUISwitchポインタとしてそれをキャストに注意)、その後、次のようにシステム制御イベントとシートンメソッドを使用することができます:

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

希望!

私が起こっていただきましたセルは、したがって、その時点でアンロードされていることであるので、一度、キーチェーンスイッチのnilを取得している、ここで起こっていただきましたが、あなたはセルがビューの外にあるときの動作を起動しようとしているということだと思いますそれはそのその再読み込みもはや皆無ので、見えてきます。しかし、私はあなたがそうのようなスイッチを割り当てていることがわかります。

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

表のセル内のスイッチへの参照を、セルがビューの外に出たとき、スイッチが、以来、ゼロになってきているので、それが作るので、あなたが、それを保持していないがアンロードされ、keychainSwitchは、結果としてゼロになってきているとしたがって、あなたのクラスのメンバーも同様にゼロになってきています。あなたは、テーブルを再構築すると、エルkeychainSwitch外のスイッチを取得し、または並べ替えのようなものの場合をCとし、その後keychainSwitchとを保持したい場合があります。ホープ、このことができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top