NSArray Updating the Table View binded to Array Controller

| | August 4, 2015

I Have an NSArray Bind to Array Controller, The Array Controller is again bind to the NSTableView.

  • Array containing Models, Each model has there property attributes defined and some have undefined key attributes.Where i would like to replace after some time or on some event.
  • I have NSModifiedArray Which is a sub class of NSArray.
  • NSModifiedArray has custom methods to addModifiedObject and removeModifiedObject.
  • After a while the objects in a array needs to replace the certain objects on an event and when i do a replace object with in the custom class(NSModifiedArray) the Array Controller doesn’t update the Table View.

So how can i update array controller with out making a call of rearrangeObjects on NSArrayController but with in the custom class that i made (NSModifiedArray Sub class of NSMutableArray). I’m just wondering how can i make a KVO call on this, If yes then how?

@interface NSModifiedArray : NSMutableArray

@implementation NSModifiedArray

/** Trying to replace the objects in an array **/
-(void)objectAtIndex:(NSUInteger)index withArray:(NSArray *)inArray
    /* A Subset of array is passed to this method, if the index passed     contains a null value then replace with the subset passed. */
    if([[super objectAtIndex:index] isKindOfClass:[NSNull class]]) {
        /* replace the subset, After successfull replace the instance of this class binded to array controller doesn't update in UI, So i'm unable to see the replaced objects on the UI, even its updated and replaced successfully. Keeping a break and doing NSLog will show me the replaced objects. */

        [self replaceObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index,[inArray count])] withObjects:inArray];

One Response to “NSArray Updating the Table View binded to Array Controller”

  1. You should make your method part of a category.

    Or, make a new class that has an NSMutableArray property.

    You don’t want to subclass NSMutableArray for what you’re doing, because there are a lot of conditions you’re not fulfilling by not overriding the default implementation of the methods listed in the NSMutableArray reference and the NSArray reference:

    Subclassing Notes

    There is typically little reason to subclass NSMutableArray. The class does well what it is designed to do—maintain a mutable, ordered collection of objects. But there are situations where a custom NSArray object might come in handy. Here are a few possibilities:

    Changing how NSMutableArray stores the elements of its collection. You might do this for performance reasons or for better compatibility with legacy code.

    Acquiring more information about what is happening to the collection (for example, statistics gathering).

    Methods to Override

    NSMutableArray defines five primitive methods:

    • insertObject:atIndex:

    • removeObjectAtIndex:

    • addObject:

    • removeLastObject

    • replaceObjectAtIndex:withObject:

    In a subclass, you must override all these methods. You must also override the primitive methods of the NSArray class.

    In the NSArray docs, it continues:

    Any subclass of NSArray must override the primitive instance methods count and objectAtIndex:. These methods must operate on the backing store that you provide for the elements of the collection. For this backing store you can use a static array, a standard NSArray object, or some other data type or mechanism. You may also choose to override, partially or fully, any other NSArray method for which you want to provide an alternative implementation.

    You might want to implement an initializer for your subclass that is suited to the backing store that the subclass is managing. If you do, your initializer must invoke one of the designated initializers of the NSArray class, either init or initWithObjects:count:. The NSArray class adopts the NSCopying, NSMutableCopying, and NSCoding protocols; if you want instances of your own custom subclass created from copying or coding, override the methods in these protocols.

    Remember that NSArray is the public interface for a class cluster and what this entails for your subclass. You must provide the storage for your subclass and implement the primitive methods that directly act on that storage.

    Alternatives to Subclassing

    Before making a custom class of NSArray, investigate NSPointerArray and the corresponding Core Foundation type, CFArray Reference. Because NSArray and CFArray are “toll-free bridged,” you can substitute a CFArray object for a NSArray object in your code (with appropriate casting). Although they are corresponding types, CFArray and NSArray do not have identical interfaces or implementations, and you can sometimes do things with CFArray that you cannot easily do with NSArray. For example, CFArray provides a set of callbacks, some of which are for implementing custom retain-release behavior. If you specify NULL implementations for these callbacks, you can easily get a non-retaining array.

    If the behavior you want to add supplements that of the existing class, you could write a category on NSArray. Keep in mind, however, that this category will be in effect for all instances of NSArray that you use, and this might have unintended consequences. Alternatively, you could use composition to achieve the desired behavior.

    I think what’s going on with the “class cluster” stuff is that Apple’s got private subclasses of these like __NSArrayI and __NSArrayM which have functionality you’d need to replicate since you cannot subclass those private subclasses.

Leave a Reply