Terry Bu
Terry Bu

Reputation: 899

How to push to a new view controller from a custom tableviewcell

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;
}

Upvotes: 1

Views: 1072

Answers (2)

Terry Bu
Terry Bu

Reputation: 899

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];
    }
}

Upvotes: 0

Choppin Broccoli
Choppin Broccoli

Reputation: 3066

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

Upvotes: 1

Related Questions