I have a UITableView
with cells that contain a UISwitch
control. It's similar to the table view in the iPhone's Clock app shown below...
我有一个UITableView与包含UISwitch控件的单元格。它类似于下面显示的iPhone时钟应用程序中的表格视图...
alt text http://epicself.com/wp-content/uploads/2009/04/1-1.jpg
替代文字http://epicself.com/wp-content/uploads/2009/04/1-1.jpg
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? I can't really do much with it without knowing it's context.
我的问题是,当用户切换开关并调用switchToggled方法时,如何判断它属于哪个表格单元?如果不知道它的背景,我真的无法做很多事情。
Thanks so much in advance for your help!
非常感谢您的帮助!
2 个解决方案
#1
9
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方法中,你可以转换传入的UISwitch(它是UITableViewCell本身)的superview,然后将它传递给tableView的indexPathForCell方法。
indexPathForCell
will return a NSIndexPath
object, which you can then use to index to your datamodel to modify. Then all you gotta do is call reloadData
on your tableView
.
indexPathForCell将返回一个NSIndexPath对象,然后您可以使用该对象索引要修改的数据模型。然后你要做的就是在tableView上调用reloadData。
Also, in cellForRowAtIndexPath
, you should set the UISwitch
's state based on your model.
此外,在cellForRowAtIndexPath中,您应该根据您的模型设置UISwitch的状态。
#2
9
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
#1
9
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方法中,你可以转换传入的UISwitch(它是UITableViewCell本身)的superview,然后将它传递给tableView的indexPathForCell方法。
indexPathForCell
will return a NSIndexPath
object, which you can then use to index to your datamodel to modify. Then all you gotta do is call reloadData
on your tableView
.
indexPathForCell将返回一个NSIndexPath对象,然后您可以使用该对象索引要修改的数据模型。然后你要做的就是在tableView上调用reloadData。
Also, in cellForRowAtIndexPath
, you should set the UISwitch
's state based on your model.
此外,在cellForRowAtIndexPath中,您应该根据您的模型设置UISwitch的状态。
#2
9
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