从UITableView传递indexPath

时间:2015-05-19 10:12:40

标签: objective-c uitableview uiviewcontroller uistoryboardsegue nsindexpath

在我的应用程序中,我有一个UITableViewController(Uctovatrida0TableViewController)和另外两个UIViewContoller(DetailViewController和示例)。

现在我遇到了问题,因为我不知道如何在我的第一个indexPath.row中识别索引(来自UIViewController),然后正确引用另一个UIViewController

我的Uctovatrida0TableViewController.m中的代码(效果很好):

-(void) showDetailsForIndexPath:(NSIndexPath*)indexPath
{
    [self.searchBar resignFirstResponder];
    DetailViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"];
    Ucty* ucet;

    if(isFiltered)
    {
        ucet = [_filteredTableData objectAtIndex:indexPath.row];
    }
    else
    {
        ucet = [self.alldataArray objectAtIndex:indexPath.row];
    }
    vc.uctyItem = ucet;
    [self.navigationController pushViewController:vc animated:true];
}

我的类DetailViewConroller中的代码,我需要来自Uctovatrida0TableViewController.m的indexPath

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Priklad"])
    {
        Priklad *vc = [segue destinationViewController];
        Ucty* ucet;

        //of course this does not work
        ucet = [self.alldataArray objectAtIndex:NSIndexPath.row];

        vc.uctyItem = ucet;
        [self.navigationController pushViewController:vc animated:true];
    }
}

我的班级Priklad.m中的代码:

- (void)setDetailItem:(id)newDetailItem
{
    if (self.uctyItem != newDetailItem)
    {
        self.uctyItem  = newDetailItem;
        [self configureView];

    }
}
- (void)configureView
{
    if (self.uctyItem ) {
        self.prikladLabel.text = self.uctyItem.priklad;
        self.uctykprikladu.text = self.uctyItem.uctykprikladu;
    }
}

3 个答案:

答案 0 :(得分:2)

从您的代码中,DetailViewConroller似乎有一个属性uctyItem。因此,在DetaiViewController的{​​{1}}方法中,您只需拨打prepareForSegue:sender:

答案 1 :(得分:1)

在推送到DetailViewConroller之前添加一个可以设置的属性。

@property (assign, nonatomic) int index;

答案 2 :(得分:1)

如果您想要选择单元格indexPath,那么有一种方法:selectedIndexPath = [tableView indexPathForSelectedRow];