Search code examples
cocoatableviewnsarraycontroller

Tell an Array Controller to refresh the table view after the array is edited?


Thanks for reading:

I've gotten stuck on this problem before and I know there is a solution, but after a a few hours of searching I haven't been able to find it again.

I'm using an NSForm and an "Add" button to create a new instance of a student and add that student to an array (sessionRoster). No problems here.

- (IBAction)addNewStudentButtonPressed:(id)sender {
//CREATE NEW STUDENT
Student *newStudent = [[Student alloc] init];

//ASSIGN VALUES FROM FORM
newStudent.firstName = [[studentForm cellAtIndex:0] stringValue];
newStudent.lastName = [[studentForm cellAtIndex:1] stringValue];

//ADD STUDENT TO ROSTER
[sessionRoster addObject:newStudent];

//CLEAR FORM
[[studentForm cellAtIndex:0] setStringValue:@""];
[[studentForm cellAtIndex:1] setStringValue:@""];
}

I'm using an Array controller to display the array in a tableview. No problems here. I have the bindings correct, because I've included some "dummy" students in the init, and they appear when the program is run (I wanted to post the screenshot, but I don't have enough reputation).

My question is, how can I make the table update each time the "add" button is pressed? I do have the table as a property, and calling [tableView reloadData] doesn't work. I believe the solution before was some kind of "contentWillUpdate:YES" and "contentDidUpdate:YES" pairing, but I can't find that information again.

Thanks for your time! J


Solution

  • Add the student object to the arrayController not to the array. The array controller "does the lifting."