【问题标题】:Determining the state of a UISwitch when the UITableCell it is on is clicked当单击 UITableCell 时确定 UISwitch 的状态
【发布时间】:2011-03-17 03:54:21
【问题描述】:

我是 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

【问题讨论】:

    标签: uitableview uiswitch didselectrowatindexpath


    【解决方案1】:
    - (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)。

    【讨论】:

    • 感谢您的解决方案。它工作得很好。至于你问我的问题,这是另一个开发者向我展示的方式。如果有更好的方法,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多