Reputation: 385
Here I have a problem figuring out to trigger a segue from within a custom UITableViewCell.
The thing is that I have implemented an 'edit' UIButton that would pass on the NSIndexPath of the UITableViewCell to the next ViewController in order to edit some entities from Core Data.
Therefore, all the methods and IBActions link are implemented inside the UITableViewClass, not in the UITableViewClass as usual. Normally, I would trigger [self performSegue: withIdentifier:]
from the ViewController.m file, but here since the IBAction method is implemented inside the UITableViewCell.m file, there's no way to access its ViewController; which later makes the [self performSegue: withIdentifier:]
impossible.
I think this is quite common, but I still couldn't come up with a good idea to solve this problem. Do you have any strategy regarding this issue?
Upvotes: 1
Views: 746
Reputation: 9915
I usually use a callback block for small things like this because it's less verbose than delegation:
@interface MyCell : UITableViewCell
@property (strong, nonatomic) void(^tapHandler)();
- (IBAction)buttonTapped;
@end
#import "MyCell.h"
@implementation MyCell
- (void)buttonTapped
{
if (self.tapHandler) {
self.tapHandler();
}
}
@end
Then you set the tap handler when you configure the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = ...;
[cell setTapHandler:^{
//tap handling logic
}];
}
Upvotes: 1