0

Key-Value Observing the to-many relationship of an NSMutableArray seems like a good technique for automatically updating a tableview that has a single section and many rows. However, my table view is grouped and more naturally fits having an array, sections, with each object within being an array containing the rows for that section. Is there any way for me to use KVO to observe the row arrays within the sections array when the number of sections is dynamic?

Ultimately, I wanted to achieve something similar to the NSFetchedResultsControllerDelegate methods without having to use Core Data:

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)object atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath

MaxGabriel
  • 7,617
  • 4
  • 35
  • 82
  • I think you have to add KVO to the parent array (sections) then add KVO to each child array inserted/removed from that array as they are added/removed. – nielsbot Jan 24 '13 at 19:00
  • 1
    `NSArray`s don't post KVO notices for insertions and deletions. You should look into using an array controller. – jscs Jan 24 '13 at 19:03
  • @nielsbot I'm not super familiar with KVO for to-many relationships, but to do observe a to-many relationship you have to specifically named methods for KVO to work, i.e. for an array named `items` you have to use `insertObject:inItemsAtIndex:` (and several others). This is the only example I've found: https://github.com/jlamarche/Old-Blog-Code/blob/master/KVOTables/Classes/RootViewController.m – MaxGabriel Jan 24 '13 at 19:04
  • @JoshCaswell This is for iOS so I can't use an `NSArrayController`, but I know I can observe the to-many relationship of the `NSArray` as discussed in this blog post http://iphonedevelopment.blogspot.com/2009/11/using-kvo-for-table-updates.html – MaxGabriel Jan 24 '13 at 19:05
  • 2
    Assuming the array you want to observe is a property of some object, mutating that property via the array returned by `-mutableArrayValueForKey:` will post KVO notifications for that property. (If that makes sense) – nielsbot Jan 24 '13 at 19:22

0 Answers0