Search code examples
objective-cmacoscocoanstableviewnsnotification

NSTableViewDelegate receiving multiple NSNotifications


I have a view-based NSTableView whose data is supplied by a subclass of NSArrayController. The array controller also receives NSNotifications (just 2) from other objects telling it to update. As long as the array controller is not acting as a delegate for the NSTableView, everything works as expected and each notification is received only once. When I attach the array controller as a delegate for the NSTableView (in IB), these same notifications are received multiple (like hundreds!) of times. The array controller is only registered once for these notifications (in awakeFromNib:).


Solution

  • For the sake of posterity.

    In Apple's 'Table View Programming Guide for Mac' is this little nugget:

    Note: Calling makeViewWithIdentifier:owner: causes awakeFromNib to be called multiple times in your app. This is because makeViewWithIdentifier:owner: loads a NIB with the passed-in owner, and the owner also receives an awakeFromNib call, even though it’s already awake.

    So, when attached as a delegate, my class' awakeFromNib: was being called for every visible NSTableView cell, resulting in the notification observer being registered each time.