Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I remove all items from an NSTableView controlled by NSArrayController?
    primarykey
    data
    text
    <p>I'm using an <code>NSArrayController</code>, <code>NSMutableArray</code> and <code>NSTableView</code> to show a list of my own custom objects (although this question probably applies if you're just showing a list of vanilla <code>NSString</code> objects too).</p> <p>At various points in time, I need to clear out my array and refresh the data from my data source. However, just calling <code>removeAllObjects</code> on my <code>NSMutableArray</code> object does not trigger the KVO updates, so the list on screen remains unchanged.</p> <p><code>NSArrayController</code> has <strong>no</strong> <code>removeAllObjects</code> method available, which seems really weird. (It does have <code>addObject</code>, which I use to add the objects, ensuring the KVO is triggered and the UI is updated.)</p> <p>The cleanest way I've managed to cause this happen correctly is:</p> <pre><code>[self willChangeValueForKey:@"myArray"]; [myArray removeAllObjects]; [self didChangeValueForKey:@"myArray"]; </code></pre> <p>...so I'm kind of having to do the KVO notification manually myself (this is in my test app class, that contains the <code>myArray</code> property, which is <code>NSMutableArray</code>, as mentioned.)</p> <p>This seems wrong - is there a better way? From my googling it seems a few people are confused by the lack of <code>removeAllObjects</code> in <code>NSArrayController</code>, but haven't seen any better solutions.</p> <p>I have seen this solution:</p> <pre><code>[self removeObjectsAtArrangedObjectIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [[self arrangedObjects] count])]]; </code></pre> <p>but this looks even more unpleasant to me. At least my solution is at least marginally self-documenting.</p> <p>Did Apple not notice that sometimes people might want to empty a list control being managed via an <code>NSArrayController</code> object? This seems kind of obvious, so I think I must be missing something...</p> <p>Aside: of course, if I add new items to the array (via <code>NSArrayController</code>), then this triggers a KVO update with the <code>NSArrayController/NSTableView</code>, but:</p> <ol> <li>Sometimes I don't put any items in the list, because there are none. So you just see the old items.</li> <li>This is a bit yucky anyway.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload