Tom Wells
Tom Wells

Reputation: 3

Accessing a UISwitch from a custom table cell

I'm currently working on my second project which is focusing on CoreData and Custom Cells.

I have a custom cell with an image & label & switch, i'm trying to save the value of the switch to userdefaults when the value of the switch has changed. But i'm stuck with how to access each switch individually (there are 2 in the same section of my table view), so that when the switch is pressed the integer stored in userdefaults is instantly updated.

This is the code in the .m file concerning the custom cell (switchCell) apologises for any messy code etc, i'm pretty much 100% self taught without any advice/feedback on any mistakes i'm making.

- (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;
    }
}

}

Thanks in advance to any help!

Upvotes: 0

Views: 773

Answers (3)

Jared Chen
Jared Chen

Reputation: 183

So many codes :-) and I'm not suer if I understand your mind or not. But I think maybe you can define two properties in your viewController for two switches, switchOne and switchTwo , and you assign them in function

tableView:cellForRowAtIndexPath:

I always do this when I have independent controls in grouped tableView.

Still I have some question about your code:

  1. Do you mean the second and third cell in your first section are cells with switches?

  2. I think in your code

    if(indexPath.row == 1 || indexPath.row == 2){
    SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCellIdentifier"];
    cell = switchCell; } the cell will always be nil, because you never build a cell with identifier "SwitchCellIdentifier"

  3. I think you build switch controls for other cells of your first section, so I totally have no idea what you want

It's 2:00 in the morning here in China, I worked all night long and I'm very tried, so maybe your code is right and I misunderstood, if so, please let me know.

Upvotes: 0

Tim Bodeit
Tim Bodeit

Reputation: 9693

Add an IBAction to your SwitchCell.

In the header file add:

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

And in the .m file apply:

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

    // Do your Core Data work here
}

You might have to add a property to SwitchCell, so that it knows what database entry to create/change.

Then go into your Storyboard or .nib file, click on the UISwitch, open the last Tab in the object inspector. There you should be able to see Send Events -> Value Changed. Drag a connection from that to your SwitchCell, select your IBAction and you should be good to go.

Upvotes: 0

Au Ris
Au Ris

Reputation: 4669

I would add that switch in your subclassed cell and not in the viewController. Then in the cell subclass create a delegate, which would call a method like switchDidChangeValue in the viewController. There set your defaults.

Upvotes: 1

Related Questions