Определение состояния UIS-переключателя при нажатии UITableCell, на котором он включен - PullRequest
0 голосов
/ 17 марта 2011

Я новичок в программировании на IOS, и у меня возникла проблема. У меня есть табличное представление, которое я динамически загружаю с помощью UISwitch в каждой ячейке, используя следующий код в cellForRowAtIndexPath

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

1 Ответ

2 голосов
/ 17 марта 2011
- (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 (Last UiSwitch).

...