0

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

4

1 に答える 1

2
- (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 = (UISwitch *)cell.accessoryView ;       

    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];
}

しかし、なぜあなたが使用しているのかわかりません。

UISwitch *mySwitch = [[UISwitch alloc]init];
self.myNewSwitch = mySwitch;
[mySwitch release];
[cell setAccessoryView: self.myNewSwitch];

新しい UITableViewCell を作成するたびに、self.myNewSwitch は同じ UISwitch (最後の UiSwitch) になります。

于 2011-03-17T04:23:45.067 に答える