How to find indexPath for tapped button in tableView Using Seque

后端 未结 3 567
别那么骄傲
别那么骄傲 2020-12-05 09:16

I have a UITableView using prototype cell.

The cells have a UIButton that show a UIViewController (Modal) using Segue

3条回答
  •  一生所求
    2020-12-05 09:43

    First make a global int variable selecteIndex;

    In your cell, give your button tag similar to cell's indexPath.row value

    E.g. btn.tag = indexPath.row;

    The assing & call action on click of that button

    [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    -(void)buttonClicked:(id)sender
    {
        UIButton *btn = (UIButton*)sender;
        selectedIndex = btn.tag;
        [self performSegueWithIdentifier:@"segueName" sender:self];
    }
    

    Then at end of your view write this function

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"segueName"]) {
            viewController *dvc = (viewController *)[segue destinationViewController];
            [dvc setSelected:selectedIndex];
        };
    }
    

提交回复
热议问题