I'm using an NSArrayController
, NSMutableArray
and NSTableView
to show a list of my own custom objects (although this question probably applies if you're just showing a list of vanilla NSString
objects too).
At various points in time, I need to clear out my array and refresh the data from my data source. However, just calling removeAllObjects
on my NSMutableArray
object does not trigger the KVO updates, so the list on screen remains unchanged.
NSArrayController
has no removeAllObjects
method available, which seems really weird. (It does have addObject
, which I use to add the objects, ensuring the KVO is triggered and the UI is updated.)
The cleanest way I've managed to cause this happen correctly is:
[self willChangeValueForKey:@"myArray"];
[myArray removeAllObjects];
[self didChangeValueForKey:@"myArray"];
...so I'm kind of having to do the KVO notification manually myself (this is in my test app class, that contains the myArray
property, which is NSMutableArray
, as mentioned.)
This seems wrong - is there a better way? From my googling it seems a few people are confused by the lack of removeAllObjects
in NSArrayController
, but haven't seen any better solutions.
I have seen this solution:
[self removeObjectsAtArrangedObjectIndexes:
[NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(0, [[self arrangedObjects] count])]];
but this looks even more unpleasant to me. At least my solution is at least marginally self-documenting.
Did Apple not notice that sometimes people might want to empty a list control being managed via an NSArrayController
object? This seems kind of obvious, so I think I must be missing something...
Aside: of course, if I add new items to the array (via NSArrayController
), then this triggers a KVO update with the NSArrayController/NSTableView
, but:
You don't remove items from a table view. It doesn't have any items—it just displays another object's items.
If you bound the array controller's content array binding to an array property of some other object, then you should be working with that property of that object. Use [[object mutableArrayValueForKey:@"property"] removeAllObjects]
.
If, on the other hand, you haven't bound the array controller's content array binding, then you need to interact with its content
directly. Use [[arrayController mutableArrayValueForKey:@"content"] removeAllObjects]
. (You could also work with arrangedObjects
instead of content
. If one doesn't work, try the other—I've only ever done things the first way, binding the array controller to something else.)
Had this problem as well and solved it this way:
NSArrayController* persons = /* your array controller */;
[[persons content] removeAllObjects];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With