私は IOS プログラミングの初心者で、問題を抱えています。cellForRowAtIndexPath で次のコードを使用して、各セルに UISwitch を動的にロードするテーブル ビューを取得しました。
UISwitch *mySwitch = [[UISwitch alloc]init];
self.myNewSwitch = mySwitch;
[mySwitch release];
[cell setAccessoryView: self.myNewSwitch];
[(UISwitch *)cell.accessoryView addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];
それはうまくいきます。スイッチをクリックすると、changeState のコードが正常に実行されます。
ここに私の問題があります。ユーザーが表のセルを押したときに、スイッチの状態 (オンまたはオフ) を確認したい。私の問題は、 didSelectRowAtIndexPath コードでそれを決定する方法がわからないことです。正しいセルは特定できますが、Google で検索したところ、そのセルにあるスイッチにアクセスする方法がわかりませんでした。didSelectRowAtIndexPath のコードは次のとおりです。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//If the on/off switch is on, go to the probes page. if off, do not do anything
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//Determine the switch state here.
UISwitch *theSwitch;
theSwitch = ??????? //<- What do I do???
if (theSwitch.isOn) {
//define the probes view controller
Probes *detailViewController = [[Probes alloc] initWithNibName:@"Probes" bundle:nil];
//pass in the ip address
detailViewController.lblIPAddr = cell.detailTextLabel.text;
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
//Deselect this row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
どんなヘルプ/コードも役に立ちます。
-NCGrimbo