문제

So I have a UITableView, and the cells are connected to a UIStoryboardSegue in order to open a new UIViewController. After clicking a cell prepareForSegue get's called and everything works fine.

QUESTION: In the follwoing method how can i know what is the indexPath of the selected cell in my TableView?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    ObjectDetailViewController *mvc = [segue destinationViewController];
    // PassingObject *obj = [self.array objectAtIndex: ? ];
    mvc.passingObject = obj;
}
도움이 되었습니까?

해결책

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Assume self.view is the table view
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    DetailObject *detail = [self detailForIndexPath:path];
    [segue.destinationViewController setDetail:detail];
}

다른 팁

Instead of [self.tableView indexPathForSelectedRow] (look at the DJPlayer answer) you can use [self.tableView indexPathForCell:sender], since sender is a selected cell in this case.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top