01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

246 CHAPTER 12 Reading and writing application data<br />

insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]<br />

withRowAnimation:UITableViewRowAnimationFade];<br />

break;<br />

case NSFetchedResultsChangeDelete:<br />

[self.tableView<br />

deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]<br />

withRowAnimation:UITableViewRowAnimationFade];<br />

break;<br />

case NSFetchedResultsChangeUpdate:<br />

[self.tableView<br />

reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]<br />

withRowAnimation:UITableViewRowAnimationFade];<br />

break;<br />

}<br />

}<br />

case NSFetchedResultsChangeMove:<br />

[self.tableView<br />

deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]<br />

withRowAnimation:UITableViewRowAnimationFade];<br />

[self.tableView<br />

insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]<br />

withRowAnimation:UITableViewRowAnimationFade];<br />

break;<br />

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {<br />

[self.tableView endUpdates];<br />

}<br />

All changes complete, so animate them<br />

What does all this code do? First, in the viewDidLoad method, you set up Edit and<br />

Add buttons. Notice that you get editButtonItem for free from UIViewController. In<br />

the addPerson method, you set up and present the view controller to add a new person.<br />

Next, in the tableView:commitEditingStyle:forRowAtIndexPath: method, you<br />

take care of deleting entries from the managed object context and save it afterwards.<br />

Finally, that really long method at the end, controller:didChangeObject:atIndex-<br />

Path:forChangeType:newIndexPath:, gets called by the NSFetchedResultsController<br />

every time the result set changes in any way. Now when you add or delete a person<br />

from the managed object context, the fetched results controller calls this method, and<br />

it updates the table view because you set the instance of PeopleViewController as the<br />

delegate of the NSFetchedResultsController in listing 12.8.<br />

Build and run your application: you should be able to add and delete people now.<br />

12.4.8 Managing tasks<br />

Okay, you can list, add, and delete people in your application. The only thing missing<br />

is the ability to add tasks to a person. To do that, you create another UITableView-<br />

Controller subclass and call it TasksViewController. It’ll look basically the same as<br />

the PeopleViewController with only a few differences (you can copy and paste the<br />

code from PeopleViewController as a starting point): it’ll have a person instance

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!