单击 UITableCell 时确定 UISwitch 的状态
我是 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];
}
任何帮助/代码都会有帮助。
-NC格林博
I a newb to IOS programming and I'm having a problem. I've got a table view that I dynamically load with a UISwitch in each cell using the following code in 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];
That works fine. When I click on the switch, the code in changeState runs fine.
Here's where my problem is. When the user presses a cell in the table, I want to see what state the switch is in (On or Off). My problem is that I can not figure out how to determine that in the didSelectRowAtIndexPath code. I can determine the correct cell, but I couldn't find out how to access the switch that is on that cell when I searched using Google. Here's my code for 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];
}
Any help/code will be helpful.
-NCGrimbo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但我不知道你为什么使用,
每次你创建新的 UITableViewCell 时, self.myNewSwitch 都是相同的 UISwitch (最后一个 UiSwitch )。
but I don't know why you are using,
every time you make new UITableViewCell, self.myNewSwitch is same UISwitch (Last UiSwitch).