12

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.

ardavis
  • 9,842
  • 12
  • 58
  • 112

4 Answers4

25

You need to implement

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



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


}
Desdenova
  • 5,326
  • 8
  • 37
  • 45
  • 1
    note that both didSelectRowAtIndexPath: and prepareForSegue: are methods of UIViewController and not of UITableViewCell – Brabbeldas Feb 26 '14 at 08:14
4

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
1

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!

heckman
  • 499
  • 3
  • 8
1

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!

Gal
  • 1,582
  • 2
  • 14
  • 30