ardavis
ardavis

Reputation: 9895

Custom UITableViewCell not calling prepareForSegue

I have a custom UITableViewCell, called EventCell.

EventCell.h

#import <UIKit/UIKit.h>

@interface EventCell : UITableViewCell

@property (nonatomic, strong) IBOutlet UILabel *titleLabel;
@property (nonatomic, strong) IBOutlet UILabel *locationLabel;
@property (nonatomic, strong) IBOutlet UILabel *dateLabel;
@property (nonatomic, strong) IBOutlet UILabel *typeLabel;
@end

EventCell.m

#import "EventCell.h"

@implementation EventCell

@synthesize titleLabel, locationLabel, dateLabel, typeLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

Here is how I'm setting up my cell.

EventsMasterViewController.m

- (EventCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Event *myEvent;
    NSString *CellIdentifier = @"EventCell";
    EventCell *cell = (EventCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EventCell" owner:nil options:nil];

    for (id currentObject in topLevelObjects)
    {
        if ([currentObject isKindOfClass:[EventCell class]])
        {
            cell = (EventCell *)currentObject;
            break;
         }
    }

    myEvent = [self.myEventsDataController objectInListAtIndex:indexPath.row];

    cell.titleLabel.text = myEvent.name;
    cell.locationLabel.text = myEvent.location;
    cell.typeLabel.text = @"Social";
    cell.layer.borderColor = [UIColor blackColor].CGColor;
    cell.layer.borderWidth = 1.0;

    return cell;
}

The cell is being formatted great, it looks exactly as I need it to. But when I click on it, the cell highlights blue and doesn't segue to the next view. I put a breakpoint in my prepareForSegue method and it's not even getting called.

Is there some way to call prepareForSegue manually? If so, where should I do that.

Upvotes: 12

Views: 9428

Answers (4)

jrturton
jrturton

Reputation: 119292

You shouldn't have to implement didSelectRow for this to work - control-dragging from a cell to another view controller should create a segue that works when tapping the cell.

In my case, the problem was that the storyboard editor was creating the segue incorrectly. I had two very similar storyboards, one worked and the other didn't, and looking at the source code (right click on the storyboard file and choose Open As -> Source Code), I saw this:

<segue destination="UWX-rF-KOc" kind="replace" identifier="showStory" trigger="accessoryAction" splitViewControllerTargetIndex="1" id="Gly-La-KIe"/>

Note the trigger attribute. This suggests to me that the segue will be fired from the accessory action of the cell. You can even see this from the connections inspector of the cell:

enter image description here

Removing this and replacing the segue by dragging from the "selection" handle above instead of control-dragging from the cell gave me the correct segue.

I'm not sure what it is about this particular cell that makes control-dragging connect to the accessory action instead of the selection action, but there you go.

Upvotes: 4

Gal
Gal

Reputation: 1582

My problem was the cell identifier.

So, in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I declared static NSString *cellIdentifier = @"cell"; and then in storyBoard I added this identifier in here:

enter image description here

And here you go!

Upvotes: 1

heckman
heckman

Reputation: 499

As stated above, you need to use didSelectRowAtIndexPath unless you configure a segue in your storyboard to a new UIViewController. This allows you to use the prepareForSegue function instead of the programmatic call of performSegueWithIdentifier.

Hope that helps clear it up!

Upvotes: 1

Desdenova
Desdenova

Reputation: 5377

You need to implement

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil];


}

Upvotes: 25

Related Questions