kurt moyer
kurt moyer

Reputation: 31

Checklist on table view won't work

I have a table View that when a cell is added, it won't work like the cells that are already on the app. When I press the new cell, my program crashes, I get this error: "SIGABRT"

And here is my code for when the cell is pressed:

- (void)tableView:(UITableView *)tableView 
  didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{

    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    [self setCurrentChosenFund: [self.cells objectAtIndex:indexPath.row]];

    [self setSelectedCell: [tableView cellForRowAtIndexPath:indexPath]];

    if  ([self.currentChosenFund valueForKey:@"Selected"] == [NSNumber 
    numberWithBool:YES])

    {
        //selectedCell.imageView.image = [UIImage imageNamed:@"checklist.png"];
        [self.selectedCell setAccessoryType: UITableViewCellAccessoryNone];
        [self.currentChosenFund setObject:[NSNumber numberWithBool:NO] 
     forKey:@"Selected"]; // ERROR IS ON THIS LINE :(
    }

    else

    {          
        //selectedCell.imageView.image = nil;  
        [self.selectedCell setAccessoryType: UITableViewCellAccessoryCheckmark];
        [self.currentChosenFund setObject:[NSNumber numberWithBool:YES] 
     forKey:@"Selected"];
    }

    [[NSUserDefaults standardUserDefaults] setObject:self.cells forKey:@"funds"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Does anyone know how to fix this?

The code for the save: method is:

- (IBAction)saveButton:(id)sender {
NSLog(@"%@", textField.text);
CheckListPracticeViewController * obj = (CheckListPracticeViewController     
         *)self.parentViewController;
[obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text      
forKey:@"name"] atIndex:0]; 

// [obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];

}

OK so I added this line of code to my saveButton: method:

    [self.currentChosenFund setObject:[NSNumber numberWithBool:NO]    
             forKey:@"Selected"];

I still get the app to crash! I've tried setting this to both YES and NO but it will not work.

Upvotes: 0

Views: 338

Answers (1)

PengOne
PengOne

Reputation: 48398

This is more of a comment than an answer, though it may turn out to be both. It makes more logical sense to use a BOOL for the key Selected.

Alternately, you can use

if ([[self.currentChosenFund valueForKey:@"Selected"] boolValue]) {
}

to avoid the comparison.

EDIT: Based on the comments, I believe the problem is with your saveButton method. You are not setting an object for the key "Selected", so when you go to retrieve it, you get an error. To fix this, set the default value to something in this method.

Upvotes: 1

Related Questions