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

BOOL isChecked; 

//check if there is checkmark there

if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){

    isChecked = YES; 

}

else {
    isChecked = NO; 
}

//adds or moves checkmark

if(isChecked == YES){

    NSLog(@"Checkmark removed...");



    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryNone; 

    lastIndexPath = indexPath; 

}


if(isChecked == NO){

    NSLog(@"Checkmark added...");

    [chosenSports addObject:[sports objectAtIndex:[indexPath row]]]; 

    NSLog(@"%@",[chosenSports objectAtIndex:0]); 

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
    newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

    lastIndexPath = indexPath; 

}


[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

}

For some reason the result of my NSLog is NULL, indicating that the "chosenSports" array is empty, but I don't know why! Any suggestions?

4
  • If your array was really empty, this would actually crash and not log null. Commented Aug 17, 2012 at 23:51
  • @omz, in Objective C there is no NullPointerException...compiler returns random garbage from memory if the pointer points to null valued address. So it won't crash, it will keep runing Commented Aug 17, 2012 at 23:53
  • 1
    @Owl I know, that's why it's actually not crashing (the array is nil, not empty). But trying to get the first object of an empty array would raise an exception. Commented Aug 17, 2012 at 23:59
  • @omz, My bad, apologies! I didn't understood at first time. Peace! Commented Aug 18, 2012 at 0:00

2 Answers 2

2

Make sure you initialize your array like so (in your viewDidLoad function):

self.chosenSports = [[NSMutableArray alloc] initWith...];
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure that your array is an instance of NSMutableArray. If that is done correctly, then you know that [sports objectAtIndex:[indexPath row]] is evaluating to zero (nil/NULL). Try querying the array for its count.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.