警告:我一直在寻找我的问题的答案,有几个接近了,但我仍然错过了一些东西。这是场景:
我想要一种在运行时根据表中的数据动态创建UITableViewCell
包含es 的方法(我可以这样做)。UISwitch
问题在于连接开关,以便在更改视图(导航离开、关闭等)时我可以获得它们的值。我尝试使用UIControlEventValueChanged
要通知的事件,但未能正确指定它,因为当点击该开关时它会转储。此外,似乎没有任何方法可以唯一地识别开关,因此如果所有事件都由单个例程(理想)处理,我无法区分它们。
所以...
如果我有一个 UITableView:
@interface RootViewController : UITableViewController
{
UISwitch * autoLockSwitch;
}
@property (nonatomic, retain) UISwitch * autoLockSwitch;
-(void) switchFlipState: (id) sender;
@end
// .m 文件:
@implementation RootViewController
// ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";
int row = 0;
NSString * label = nil;
TableCellDef_t * cell_def = nil;
row = indexPath.row;
cell_def = &mainMenuTableCellsDef[ row ];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
label = (NSString *) mainMenuTableCellsDef[indexPath.row].text;
[cell.textLabel setText:(NSString *) mainMenuItemStrings[ indexPath.row ]];
if (cell_def->isSpecial) // call special func/method to add switch et al to cell.
{
(*cell_def->isSpecial)(cell ); // add switch, button, etc.
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
}
and this is the 'special' function:
-(void) autoLockSpecialItem :(UITableViewCell *) cell
{
autoLockSwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[autoLockSwitch addTarget:self action:@selector(switchFlipState:) forControlEvents:UIControlEventValueChanged ];
[cell addSubview:autoLockSwitch];
cell.accessoryView = autoLockSwitch;
}
最后:
-(void) switchFlipState: (id) sender
{
NSLog(@"FLIPPED");
}
==================================================== =============
问题:
为什么当开关被点击时它会崩溃(错误的选择器)?我相信我的代码遵循我见过的所有示例代码,但显然有问题。
我不能将实例方法作为函数指针放入表中;而且它似乎也不喜欢类方法。如果我将其设为“C/C++”函数,如何访问类/实例成员变量?也就是说,如果我想调用
autoLockSpecialItem
一个静态表(或合理的传真)以便我可以获得autoLockSwitch
成员变量?如果我将其设为类方法并将autoLockSwitch
var 设为静态,那是否有效?更简单地说:我如何连接
UIControlEventValueChanged
到我的视图(我已经尝试过但失败了),我可以在运行时在事件处理程序中区分哪个开关已更改?有没有更好的办法?我不敢相信我是第一个必须解决这类问题的人。
对篇幅表示歉意,感谢关注并感谢任何和所有帮助。
:bp: