UITableView 中的 UISwitch 在滚动时重置为原始值

发布于 2024-11-10 03:34:22 字数 1135 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-11-17 03:34:22

在头文件中,声明一个 NSString 来保存开关的状态。
我们将其命名为theSwitchPosition

然后,在触发开关时运行的方法中,使 SwitchPosition 保存开关的状态:

theSwitchPosition = [NSString stringWithFormat:@"%@", switchControl.on ? @"ON" : @"OFF"];

之后,在创建 UISwitch 的方法中,根据保存数据的字符串设置开关的状态:

if ([theSwitchPosition isEqualToString:@"ON"]) {
    mySwitch.on = YES;
} else {
    mySwitch.on = NO;
}

这将起作用如果你的表视图中只有一个 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:

theSwitchPosition = [NSString stringWithFormat:@"%@", switchControl.on ? @"ON" : @"OFF"];

After that, in the method where you create the UISwitch, set the switch's status based on the string that holds the data:

if ([theSwitchPosition isEqualToString:@"ON"]) {
    mySwitch.on = YES;
} else {
    mySwitch.on = NO;
}

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.

深空失忆 2024-11-17 03:34:22

我假设您是说,如果您向下滚动(开关单元格滚动到屏幕外),然后向上滚动(开关单元格滚动回到屏幕上),则不会保留开关的状态。这是正确的吗?

如果是这样,我猜问题是细胞正在被回收和重新创建。当您从 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 different CellIdentifier.

If this is still unclear, please paste your code for tableView:cellForRowAtIndexPath: and I will try to help you further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文