1

I'm using Storyboard. I've read on SO that you cannot use segues when you are dealing with custom tableviewcells and as expected, it doesn't work. Even when I select a custom cell, it doesn't trigger anything.

So I've been trying to use didSelectRowAtIndexPath instead, using below code, but now it just pushes a completely blank screen with a black background. Obviously, I'm doing something wrong. How can I use a "selection row event" using a custom tableviewcell to push to a new view controller???

I will add the fact that SecondViewController is declared and designed in Storyboard. I've removed its segue so it's just floating in storyboard.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *svc = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:svc animated:YES];
}

Some more code below for reference.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleCellIdentifier = @"JokeCustomCell";
    JokeCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleCellIdentifier];

    JokePL *joke = [self.jokeDataManager.jokes objectAtIndex:indexPath.row];

    cell.titleLabel.text = [NSString stringWithFormat: @"%@", joke.title];
    cell.scoreLabel.text = [NSString stringWithFormat: @"Score: %@", [self quickStringFromInt:joke.score]];
    cell.timeLabel.text = [self turnSecondsIntegerIntoMinuteAndSecondsFormat:joke.length];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"M/dd/yy"];

    cell.dateLabel.text = [NSString stringWithFormat: @"%@", [dateFormatter stringFromDate:joke.creationDate]];


    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
Terry Bu
  • 889
  • 1
  • 14
  • 31

2 Answers2

1

You should totally be able to use Storyboard to push a new view controller from a custom cell. I did just that the other day. Simply Option-Click from the custom cell to the new view controller in the Storyboard

Choppin Broccoli
  • 3,048
  • 2
  • 21
  • 28
  • that's really weird ... maybe it's because I used [self.tableView registerNib:[UINib nibWithNibName:@"JokeCustomCell" bundle:nil] forCellReuseIdentifier:@"JokeCustomCell"]; in the viewDidLoad? – Terry Bu Nov 04 '14 at 22:42
  • I'm guessing I set up something incorrectly while implementing this custom tableViewCell?? – Terry Bu Nov 04 '14 at 22:43
  • Ah I see. Yeah, I'm using a prototype cell within the table view in the Storyboard – Choppin Broccoli Nov 04 '14 at 22:43
  • And then I went into the Prototype cell inside the viewcontroller in storyboard and changed its identifier and class type to JokeCustomCell – Terry Bu Nov 04 '14 at 22:44
  • http://stackoverflow.com/questions/9245969/in-a-storyboard-how-to-make-a-custom-cell-for-use-with-multiple-controllers this is the SO discussion where the best answer mentions at the end "you cannot connect storyboard segues to stand-alone Xib cells – Terry Bu Nov 04 '14 at 22:53
0

I realized that a lot of problems occur when you are using storyboard, and you are trying to mix "segues" with "didselectrowatindexpath"-style of pushing views. You gotta go one way or other. When I converted over to segues completely, it all works flawlessly.

My current approach for now is just NOT USE didSelectRowAtIndexPath and doing everything with segues instead for storyboard, like below. Just connect everything using storyboard (like ctrl-dragging from custom cell to the next view controller and naming the segue) and use below. You don't have to push or allocate your viewcontrollers unnecessarily.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];    
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        vc.whatevercustomproperty = whateverobjectarrayyouhave[path];
    }
}
Terry Bu
  • 889
  • 1
  • 14
  • 31