Custom UITableViewCell not calling prepareForSegue

前端 未结 4 973
刺人心
刺人心 2021-02-14 00:00

I have a custom UITableViewCell, called EventCell.

EventCell.h

#import 

@interface EventCell : UITabl         


        
相关标签:
4条回答
  • 2021-02-14 00:12

    You need to implement

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil];
    
    
    }
    
    0 讨论(0)
  • 2021-02-14 00:23

    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.

    0 讨论(0)
  • 2021-02-14 00:24

    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!

    0 讨论(0)
  • 2021-02-14 00:34

    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!

    0 讨论(0)
提交回复
热议问题