从自定义表格单元格访问 UISwitch



我目前正在做我的第二个项目,重点是CoreData和自定义单元格。

有一个带有图像和标签和开关的自定义单元格,当开关的值发生变化时,我正在尝试将开关的值保存到用户默认值。但是我坚持如何单独访问每个开关(我的表视图的同一部分中有 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;
    }
}
}

提前感谢任何帮助!

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

将 IBAction 添加到您的SwitchCell 中。

在头文件中添加:

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

并在 .m 文件中应用:

- (IBAction)switchChanged: (UISwitch*)sender {
    BOOL value = sender.isOn;
    // Do your Core Data work here
}

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

然后进入情节提要或 .nib 文件,单击 UISwitch,在对象检查器中打开最后一个选项卡。在那里你应该能够看到Send Events -> Value Changed.将连接从该连接到您的SwitchCell,选择您的 IBAction,您应该很高兴。

这么多代码:-)我是否理解你的心思,我不会生气。但我认为也许您可以在视图中为两个开关定义两个属性,switchOne 和 switchTwo ,并在函数中分配它们

tableView:cellForRowAtIndexPath:

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

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

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

  2. 我认为在您的代码中

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

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

现在是凌晨2:00,我通宵工作,我很努力,所以也许你的代码是对的,我误解了,如果是这样,请告诉我。

最新更新