Reputation: 220
I have a UICollectionView
and within this I have a custom UICollectionViewCell
which has a button with an action attached to it. The action deletes the object from core data.
I am then wanting to perform a [self reloadData]
. But how would I call this method from within the UICollectionViewCell
Class?
Upvotes: 1
Views: 237
Reputation: 698
This is a Swift 4+ version of J. Lopes's answer:
Place this inside the IBAction
of your UICollectionViewCell
:
NotificationCenter.default.post(name: Notification.Name(rawValue: "notification_name"), object: nil)
Place this inside the viewDidLoad()
of the receiving class:
NotificationCenter.default.addObserver(self, selector: #selector(reloadDataNow), name: Notification.Name(rawValue: "notification_name"), object: nil)
And this is the function you want to trigger in the receiving class once IBAction
is pressed from the UICollectionViewCell
:
@objc func reloadDataNow(_ notification: Notification) {
self.yourCollectionView.reloadData()
}
Upvotes: 0
Reputation: 127
You can create delete or notification in UIViewController
and in cellforItemAtindexPath
set that delegate to UIViewController
.
In button action inside UICollectionViewCell
performing operation (deletes the object) and call delegate
when receiving delegate callback in UIViewController
perform [collectionView reload];
Upvotes: 0
Reputation: 1345
Yes you can call it in your IBAction inside of your UICollectionViewCell
:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_name" object:nil];
Inside of the class you want to reload, you can put this in viewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadDataNow)
name:@"notification_name"
object:nil];
Inside of the same class you want to reload create this new method reloadDataNow
- (void)reloadDataNow {
[self.yourCollectionView reloadData];
}
I hope this can help you.
Upvotes: 2