UITableView 中的 UISwitch 在滚动时重置为原始值
我有一个 UITableview ,其中包含自定义单元格。其中一个自定义单元中有一个 UISWITCH。当滚动表视图时,即使我将其设置为“开”,开关的状态也会重置。如何在滚动过程中保持开关的状态。任何帮助表示赞赏。
-(IBAction)sameDriver:(id)sender{
if ([sender isOn]){
NSLog(@"%@",(otherdriver.drive ? @"YES" : @"NO"));
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool: YES forKey: K_SWITCH_KEY];
[defaults synchronize];
Switchon = [defaults boolForKey: K_SWITCH_KEY];
if(Switchon){
otherdriver.dfname.text = fname;
otherdriver.dlname.text = lname;
otherdriver.demail.text = email;
otherdriver.dpnum.text = phone;
}
}
else if(![sender isOn]){
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool: NO forKey: K_SWITCH_KEY];
[defaults synchronize];
NSLog(@"%@",(otherdriver.drive ? @"YES" : @"NO"));
Switchon = [defaults boolForKey: K_SWITCH_KEY];
otherdriver.dfname.text = drfname;
otherdriver.dlname.text = drlname;
otherdriver.demail.text = dremail;
otherdriver.dpnum.text = drphone;
}}
我正在 IB 中设置 UISwitch。它位于自定义 UITableviewcell 内。
谢谢
I have a UITableview with custom cells in it. One of the customcell has a UISWITCH in it. When the scroll the table view the state of the switch gets reset even if I set it to ON. How can I maintain the state of the switch during scroll. Any help is appreciated.
-(IBAction)sameDriver:(id)sender{
if ([sender isOn]){
NSLog(@"%@",(otherdriver.drive ? @"YES" : @"NO"));
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool: YES forKey: K_SWITCH_KEY];
[defaults synchronize];
Switchon = [defaults boolForKey: K_SWITCH_KEY];
if(Switchon){
otherdriver.dfname.text = fname;
otherdriver.dlname.text = lname;
otherdriver.demail.text = email;
otherdriver.dpnum.text = phone;
}
}
else if(![sender isOn]){
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool: NO forKey: K_SWITCH_KEY];
[defaults synchronize];
NSLog(@"%@",(otherdriver.drive ? @"YES" : @"NO"));
Switchon = [defaults boolForKey: K_SWITCH_KEY];
otherdriver.dfname.text = drfname;
otherdriver.dlname.text = drlname;
otherdriver.demail.text = dremail;
otherdriver.dpnum.text = drphone;
}}
i am setting the UISwitch in IB. It is inside a custom UITableviewcell.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在头文件中,声明一个 NSString 来保存开关的状态。
我们将其命名为theSwitchPosition。
然后,在触发开关时运行的方法中,使 SwitchPosition 保存开关的状态:
之后,在创建 UISwitch 的方法中,根据保存数据的字符串设置开关的状态:
这将起作用如果你的表视图中只有一个 UISwitch。如果有多个,则需要使用 NSMutableArray 代替。
In your header file, declare an NSString to hold the status of the switch.
Let's name it theSwitchPosition.
Then, in your method which runs when the switch is triggered, make theSwitchPosition hold the status of the switch:
After that, in the method where you create the UISwitch, set the switch's status based on the string that holds the data:
This will work if you have only one UISwitch in your table view. If you have more than one, you need to use NSMutableArray instead.
我假设您是说,如果您向下滚动(开关单元格滚动到屏幕外),然后向上滚动(开关单元格滚动回到屏幕上),则不会保留开关的状态。这是正确的吗?
如果是这样,我猜问题是细胞正在被回收和重新创建。当您从
dequeReusableCellWithIdentifier:CellIdentifier
获取单元格时,就会发生这种情况。要解决此问题,您需要为特殊单元格指定不同的CellIdentifier
。如果仍然不清楚,请粘贴您的
tableView:cellForRowAtIndexPath:
代码,我将尽力为您提供进一步帮助。I assume you're saying that if you scroll down (switch cell gets scrolled off screen), and then you scroll back up (switch cell gets scrolled back on screen) then the state of the switch isn't preserved. Is this correct?
If so, I would guess the issue is that the cell is being recycled and recreated. This happens when you get your cell from
dequeReusableCellWithIdentifier:CellIdentifier
. To fix this, you will need to give your special cell a differentCellIdentifier
.If this is still unclear, please paste your code for
tableView:cellForRowAtIndexPath:
and I will try to help you further.