Anita Nagori
Anita Nagori

Reputation: 747

Remove NSNotification Observer from UITableViewCell

I have UIVIewController and In that I have placed UITableView
UITableView Contain cell where cell contains UICollectionView
Each cell has its own collectionView.
Now, I want to update some rows of collection view of different sections based on tableview header.
My tableview contains section Header and button inside

Eg : When I click on TableView header I want to update some of row of its cell's collectionView so here I have using the NSNotification for callToUpdate rows in collectionView (All collectionView Related stuff is placed on cell.m file of UITableView)

But the issue is that as cell scroll , the observer is keep adding and my post notification method called multiple time.

I also wrote code for remove observer but it did not work.

Upvotes: 0

Views: 1321

Answers (1)

Austin Michael
Austin Michael

Reputation: 490

NotificationCenter will behave like this only, for example if you are using NotificationCenter.default.addObserver in viewWillAppear you have to remove the observer by using NotificationCenter.default.removeObserver in viewWillDisAppear. This is the usage description by Apple for NotificationCenter.

As cellForRowAtIndexPath will be called for multiple times, the observer also added for multiple times. Better advice is to use Delegate. when tapping on header you have to call the delegate method to do the desired action.

Upvotes: 0

Related Questions