简体   繁体   English

如果我将UISwitch控件添加到每个表视图单元格中,如何判断它属于哪个单元格?

[英]If I add a UISwitch control to each of my table view cells, how can I tell which cell it belongs to?

I have a UITableView with cells that contain a UISwitch control. 我有一个UITableView与包含UISwitch控件的单元格。 It's similar to the table view in the iPhone's Clock app shown below... 它类似于下面显示的iPhone时钟应用程序中的表格视图......

替代文字
(source: epicself.com ) (来源: epicself.com

In my app's cellForRowAtIndexPath method, I create and attach the UISwitch control like so... 在我的应用程序的cellForRowAtIndexPath方法中,我创建并附加UISwitch控件,如此...

 CGRect frameSwitch = CGRectMake(215.0, 10.0, 94.0, 27.0);
 UISwitch *switchEnabled = [[UISwitch alloc] initWithFrame:frameSwitch];
 [switchEnabled addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];

 cell.accessoryView = switchEnabled;

My question is, when the switch is toggled by the user and the switchToggled method is called, how can I tell which table cell it belongs to? 我的问题是,当用户切换开关并调用switchToggled方法时,如何判断它属于哪个表格单元? I can't really do much with it without knowing it's context. 如果不了解它的背景,我真的无法做很多事情。

Thanks so much in advance for your help! 非常感谢您的帮助!

First of all, fix the memory leak: 首先,修复内存泄漏:

UISwitch *switchEnabled = [[[UISwitch alloc] initWithFrame:frameSwitch] autorelease];

Then pick one of these options: 然后选择以下选项之一:

switchEnabled.tag = someInt; // before adding it to the cell

NSLog(@"switched %d",switch.tag); // to see which switch was toggled

or 要么

UITableViewCell *parentCell = switchEnabled.superview;
// + some magic to see which cell this actually is

In your action method, you can cast the superview of the passed-in UISwitch (which is the UITableViewCell itself), then pass that to the tableView 's indexPathForCell method. 在你的action方法中,你可以转换传入的UISwitchindexPathForCell superview (它是UITableViewCell本身),然后将它传递给tableViewindexPathForCell方法。

indexPathForCell will return a NSIndexPath object, which you can then use to index to your datamodel to modify. indexPathForCell将返回一个NSIndexPath对象,然后您可以使用该对象索引要修改的数据模型。 Then all you gotta do is call reloadData on your tableView . 然后你要做的就是在tableView上调用reloadData

Also, in cellForRowAtIndexPath , you should set the UISwitch 's state based on your model. 此外,在cellForRowAtIndexPath ,您应该根据您的模型设置UISwitch的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将UISwitch添加到分组表视图中的单元格? - How to add a UISwitch to a cell in a grouped table view? 如何为我的表格单元创建一个附件视图? - How can I create an accessory view for my table cell like this? 如何区分用户点击我的表格视图单元格和用户点击该单元格中包含的子视图之间的区别? - How do I tell the difference between the user tapping my table view cell and the user tapping a subview contained within the cell? 如何使用按钮来控制根视图中的导航,而不是表格单元格? - How Can I Use Buttons to Control Navigation in Root View, Instead of Table Cells? 如何知道我的XCode将部署到哪些iOS版本? - How can I tell which iOS versions my XCode will deploy to? 如何让我的UISwitch调用此方法? (使用theos) - How can I get my UISwitch to call this method? (using theos) 如何让UISwitch做某事? - How can I get my UISwitch to do something? 如何判断我的视图控制器是否会从内存中释放? - How can I tell if my view controller will be released from memory? 如何在Table View ViewDidLoad方法中调用自定义单元格中声明的UISwitch? - How to call UISwitch declared in Custom Cell in Table View ViewDidLoad method ? iOS:如何通过此方法为每个单元分配标签? - iOS: how can I assign each of my cells a tag in this method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM