UISwitch内定制用表不可
-
19-09-2019 - |
题
我有一个 UISwitch
内部一个自定义 UITableViewCell
(子类的这是我的呼叫 RootLabeledSwitchTableCell
).
该单元包含一个 UILabel
和 UISwitch
彼此相邻。
我有一个 @property
叫 keychainSwitch
这一点的开关在这个定义的细胞:
@interface RootLabeledSwitchTableCell : UITableViewCell {
IBOutlet UILabel *textLabel;
IBOutlet UISwitch *labeledSwitch;
}
@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UISwitch *labeledSwitch;
@end
在我的桌查委托,我已经增加了一个选择,被称为如果开关状态被翻转:
- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]];
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
switch (indexPath.section) {
case(kMyFirstSection): {
switch (indexPath.row) {
case(kMyFirstSectionFirstRow) {
[cellOwner loadMyNibFile:@"RootLabeledSwitchTableCell"];
cell = (RootLabeledSwitchTableCell *)cellOwner.cell;
self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch];
[self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched) forControlEvents:UIControlEventValueChanged];
break;
}
// ...
}
}
// ...
}
}
所以这个选择正常工作:
- (void) keychainOptionSwitched {
NSLog(@"Switched keychain option from %d to %d", ![self.keychainSwitch isOn], [self.keychainSwitch isOn]);
}
然而,我不能使用 UISwitch
实例 -setOn:animated:
初始化方法的其初始状态:
- (void) updateInterfaceState {
BOOL isFirstTimeRun = [[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue];
if (isFirstTimeRun) {
[self.keychainSwitch setOn:NO animated:NO];
}
}
从测试, self.keychainSwitch
是 nil
这是造成 -setOn:animated
什么都不做,但我仍然可以操作的开关和 NSLog
声明正确地打印的开关状态的变化,例如:
[Session started at 2009-08-24 07:04:56 -0700.]
2009-08-24 07:04:58.489 MyApp[29868:20b] keychain switch is: nil
2009-08-24 07:05:00.641 MyApp[29868:20b] Switched keychain option from 1 to 0
2009-08-24 07:05:01.536 MyApp[29868:20b] Switched keychain option from 0 to 1
2009-08-24 07:05:02.928 MyApp[29868:20b] Switched keychain option from 1 to 0
是不是有什么我是缺少关于设置 self.keychainSwitch
在 UITableView
委托法?
解决方案
我做的这一段时间以前。你应该改变你的selctor
[self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched:) forControlEvents:UIControlEventValueChanged];
然后更新你的方法
-(void) keychainOptionSwitched:(id)sender {
UISwitch *tempSwitch = (UISwitch *)sender;
}
现在tempSwitch是关于被改变了。
其他提示
我花了一年龄段的摆弄周围与定义UITableViewCells用一个简单的标签和开关上之前我发现我可以 增加一个UISwitch作为一个附件,图.你可能知道这无论如何,想要的定义的细胞用于其他原因,但仅在情况下,我想提这个!
你这么做如下(在,cellForRowAtIndexPath方法):
UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
该accessoryView位也照顾的大小和位置所以我们可以摆脱CGRectZero.
你们然后能够利用该系统控制的事件和setOn方法如下所示(注意,我们把它作为UISwitch指针,我们知道它是):
[(UISwitch *)cell.accessoryView setOn:YES]; // Or NO, obviously!
[(UISwitch *)cell.accessoryView addTarget:self action:@selector(mySelector)
forControlEvents:UIControlEventValueChanged];
希望这可以帮助!
我觉得发生了什么这里就是你正在尝试援用时的操作单元是出视,所以什么事是,该单元被卸在那个时候因此你得到一个无钥匙串的开关,一旦涉及到查看然后它没有再为零,因为它的重新加载。但我看到你指定的开关一样
self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch];
它的一个参照关于表中的细胞和你是不是保留,因此它使得从那开关是成为零,因为,当单元进行的查看是卸载和keychainSwitch成为无结果,因此类件成为零。你可能想保留keychainSwitch然后在重建你的表c的英语,得到开关了keychainSwitch,或者东西。希望这可以帮助