0

我目前正在从事我的第二个项目,该项目专注于 CoreData 和自定义单元格。

我有一个带有图像和标签和开关的自定义单元格,当开关的值发生变化时,我正在尝试将开关的值保存到 userdefaults。但是我不知道如何单独访问每个开关(在我的表格视图的同一部分中有 2 个),因此当按下开关时,存储在 userdefaults 中的整数会立即更新。

这是 .m 文件中关于自定义单元(switchCell)的代码,为任何混乱的代码等道歉,我几乎 100% 自学,没有任何关于我所犯错误的建议/反馈。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = nil;

// Code for the first section, controlling which cells use the custom cell SwitchCell
if (indexPath.section == 0)
{
    if(indexPath.row == 1 || indexPath.row == 2)
    {
        SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCellIdentifier"];
        cell = switchCell;
    }

    else
    {
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellIdentifier"];
            UISwitch *testSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
            cell.accessoryView = testSwitch;
        }
    }

}
// Each other section currently uses the standard cell type
else
{
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellIdentifier"];
    }

}

[self configureCell:cell atIndexPath:indexPath];

return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{

NSDictionary *dictionary = [settingsTableData objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Settings"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
SwitchCell *switchCell = (SwitchCell *)cell;

[[NSUserDefaults standardUserDefaults] synchronize];

// Integers to store the on/off state of each switch (2)
NSInteger capsValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"capitalsSwitchOn"];
NSInteger numbersValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"numbersSwitchOn"];
NSLog(@"Upon load capsValue equals : %d", capsValue);
NSLog(@"upon load numbersValue equals : %d", numbersValue);

// Setting individual cell values for attributes such as image and text
if (indexPath.section == 0)
{
    if(indexPath.row == 1)
    {
        switchCell.switchCellLabel.text = cellValue;
        switchCell.switchCellImage.image = [UIImage imageNamed:@"capitalsImage.jpg"];

        // Set to true or false depending on what you want the default value to be.
        //switchCell.switchCellSwitch.on = FALSE;

        if (capsValue == 1) {
            [switchCell.switchCellSwitch setOn:NO animated:YES];
        } else
            [switchCell.switchCellSwitch setOn:YES animated:YES];

    }
    else if (indexPath.row == 2)
    {
        switchCell.switchCellLabel.text = cellValue;
        switchCell.switchCellImage.image = [UIImage imageNamed:@"capitalsImage.jpg"];

        if (numbersValue == 1) {
            [switchCell.switchCellSwitch setOn:NO animated:YES];
        } else
            [switchCell.switchCellSwitch setOn:YES animated:YES];
    }
    else
    {
        cell.textLabel.text = cellValue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}
else if (indexPath.section == 1)
{
    if (indexPath.row == 0)
    {
        cell.textLabel.text = cellValue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = cellValue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}
else if (indexPath.section == 2)
{
    if (indexPath.row == 0)
    {
        cell.textLabel.text = cellValue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if (indexPath.row == 1 || indexPath.row == 2)
    {
        cell.textLabel.text = cellValue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

}

提前感谢任何帮助!

4

3 回答 3

1

我会在您的子类单元格中添加该开关,而不是在 viewController 中。然后在 cell 子类中创建一个委托,它会调用switchDidChangeValueviewController 中的方法。那里设置你的默认值。

于 2013-09-05T17:38:03.850 回答
0

这么多代码:-),如果我理解你的想法,我不会生气。但我认为也许您可以在 viewController 中为两个开关 switchOne 和 switchTwo 定义两个属性,然后在函数中分配它们

tableView:cellForRowAtIndexPath:

当我在分组的 tableView 中有独立控件时,我总是这样做。

我仍然对您的代码有一些疑问:

  1. 您的意思是第一部分中的第二个和第三个单元格是带开关的单元格吗?

  2. 我认为在你的代码中

    if(indexPath.row == 1 || indexPath.row == 2){
    SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCellIdentifier"];
    细胞=开关细胞;单元格将始终为零,因为您永远不会使用标识符“SwitchCellIdentifier”构建单元格

  3. 我认为您为第一部分的其他单元格构建了开关控件,所以我完全不知道您想要什么

现在是中国的凌晨 2:00,我通宵工作,非常努力,所以也许你的代码是正确的,我误解了,如果是,请告诉我。

于 2013-09-05T18:03:51.770 回答
0

将 IBAction 添加到您的SwitchCell.

在头文件中添加:

- (IBAction)switchChanged: (UISwitch*)sender;

并在 .m 文件中应用:

- (IBAction)switchChanged: (UISwitch*)sender {
    BOOL value = sender.isOn;

    // Do your Core Data work here
}

您可能必须向 中添加一个属性SwitchCell,以便它知道要创建/更改的数据库条目。

然后进入您的 Storyboard 或 .nib 文件,单击 UISwitch,在对象检查器中打开最后一个选项卡。在那里你应该可以看到Send Events -> Value Changed。将连接从那里拖到您的SwitchCell,选择您的 IBAction ,您应该一切顺利。

于 2013-09-05T17:38:07.800 回答