如何在我的每个 UITableView 单元格中检索 UISwitch 状态? [英] How to retrieve UISwitch state in each of my UITableView cells?

查看:53
本文介绍了如何在我的每个 UITableView 单元格中检索 UISwitch 状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在每个 TableView 单元格中都设置了一个 UISwitch 作为附件视图.

I have a UISwitch set as the accessoryView in each of my TableView cells.

如果我按下我的确认按钮,我想用 NSUserDefaults 保存每个 UISwitch 的状态.然后当我离开并返回该视图控制器时,我应该能够加载那些保存的状态,这些状态对于每个单元格都不同(打开或关闭,如图所示).

If I press my Confirm button, I want to save the state of each UISwitch with NSUserDefaults. Then when I leave and go back to that View Controller, I should be able to load those saved states which will be different for each cell (either on or off, as shown in image).

我快到了,但我想我不确定如何使用正确的 indexPath.row 保存/加载,因此它无法正常工作.现在它只是保存/加载一个 BOOL 值,所以如果我在开关打开的情况下保存一个单元格,那么它们都将打开,反之亦然.

I'm almost there but I guess I am not sure how to save/load with the right indexPath.row so it's not working correctly. Right now it is just saving/loading one BOOL value only, so if I save one cell with the switch ON, then all of them will be ON, and vice versa.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    BOOL menuSwitchState = [[NSUserDefaults standardUserDefaults] boolForKey:@"menuItemSwitch"];
    NSLog(@"Menu Switch State is: %@", menuSwitchState ? @"Yes": @"No");
    [self.switchView setOn:menuSwitchState animated:YES];

}

我的 cellForRowAtIndexPath 中的 UISwitch 代码:

UISwitch code in my cellForRowAtIndexPath:

   // Add a UISwitch to the accessory view.
    self.switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = self.switchView;
    self.switchView.tag = indexPath.row;
    self.switchView.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"menuItemSwitch"];
    [self.switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

设置 BOOL 的切换操作方法:

Switch action method which sets a BOOL:

- (void) switchChanged:(id)sender {
    UISwitch *switchControl = sender;
    NSLog(@"The switch's tag number is %ld", (long)switchControl.tag);
//    NSLog(@"The switch is %@", switchControl.on ? @"ON" : @"OFF" );


    if ([sender isOn])
    {
        self.switchIsOn = YES;
        NSLog(@"THE SWITCH IS ON");
    }
    else
    {
        self.switchIsOn = NO;
        NSLog(@"THE SWITCH IS OFF");
    }
}

应保存开关状态的确认按钮:

Confirm button that should save the state of the switch:

#pragma mark - UIBUTTONS
- (IBAction)onConfirmMenuButtonPressed:(id)sender
{
    [self performSegueWithIdentifier:@"segueMenuToGettingStarted" sender:self];


    //TODO: Save state of Switch.
    if (self.switchIsOn == YES)
    {
        [[NSUserDefaults standardUserDefaults] setBool:self.switchView.on forKey:@"menuItemSwitch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else
    {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setBool:NO forKey:@"menuItemSwitch"];
    }



}

推荐答案

您可以像在表视图中使用的任何其他值一样保存该值——保存在作为数据源的数组中.鉴于您在图像中显示的内容,您的数据源应该是一个包含菜单项、价格和开关状态键的字典数组.在 cellForRowAtIndexPath 中,您将根据数组中的值(一个 BOOL)设置开关的状态.

You save that value just like any other value you use in a table view -- in an array that's your data source. Given what you show in your image, your data source should be an array of dictionaries with keys for the menu item, price, and switch state. In cellForRowAtIndexPath, you would set the state of the switch based on the value ( a BOOL) in your array.

这篇关于如何在我的每个 UITableView 单元格中检索 UISwitch 状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆