简体   繁体   English

如何从自定义tableviewcell推送到新的视图控制器

[英]How to push to a new view controller from a custom tableviewcell

I'm using Storyboard. 我正在使用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. 我已经读过SO,您在处理自定义tableviewcell时不能使用segues,并且按预期,它不起作用。 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. 所以我一直在尝试使用didSelectRowAtIndexPath来代替,使用下面的代码,但是现在它只推送了一个黑色背景的完全空白的屏幕。 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??? 如何使用自定义tableviewcell使用“选择行事件”来推送到新的视图控制器?

I will add the fact that SecondViewController is declared and designed in Storyboard. 我将添加一个事实,即在Storyboard中声明并设计了SecondViewController。 I've removed its segue so it's just floating in storyboard. 我已经删除了它的segue,所以它只是漂浮在情节提要中。

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

You should totally be able to use Storyboard to push a new view controller from a custom cell. 您应该完全能够使用Storyboard从自定义单元中推送新的视图控制器。 I did just that the other day. 前几天我就是这样做的。 Simply Option-Click from the custom cell to the new view controller in the Storyboard 只需在自定义单元中按住Option键单击,即可在情节提要中单击新的视图控制器

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. 我意识到在使用情节提要时会出现很多问题,并且您试图将“ segues”与“ didselectrowatindexpath”风格的推送视图混合使用。 You gotta go one way or other. 你得走一条路。 When I converted over to segues completely, it all works flawlessly. 当我完全转换为segue时,一切都完美无缺。

My current approach for now is just NOT USE didSelectRowAtIndexPath and doing everything with segues instead for storyboard, like below. 我目前的当前方法是仅不使用didSelectRowAtIndexPath并使用segues代替故事板来进行所有操作,如下所示。 Just connect everything using storyboard (like ctrl-dragging from custom cell to the next view controller and naming the segue) and use below. 只需使用情节提要板连接所有内容(例如从自定义单元格ctrl拖动到下一个视图控制器并命名segue)并在下面使用即可。 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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个视图推一个新视图controller - How to push a new view controller from a view 如何从View Controller隔离到TableViewCell - How to Segue from View Controller to TableViewCell 在自定义TableViewCell内从didSelectItemAt自定义CollectionViewCell更改视图控制器 - change view controller from didSelectItemAt custom CollectionViewCell inside a custom TableViewCell 如何在目标c中的视图控制器上获取自定义TableViewCell值 - How to get custom TableViewCell value on view controller in objective c 如何从TableViewCell上的Custom CollectionViewCell推送VC? - How to push VC from Custom CollectionViewCell Which is on TableViewCell? 如何从主视图控制器在详细视图控制器中推送新视图? - How to push new view in detail view controller from master view controller? 从目标View Controller推送新的View Controller(swift) - Push new View Controller from destination View Controller (swift) 如何从情节提要中的子视图(另一个视图控制器)内的按钮推动新的视图控制器 - How can I push a new View Controller from a button within a subView (another View Controller) in Storyboard 从Swift中的TableViewCell类切换视图控制器 - Switch View Controller From TableViewCell Class in Swift 从父视图控制器在容器中设置tableViewCell - Setting tableViewCell in a container from a parent view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM