如何从可展开/可折叠 UITableView 中创建的表视图单元格转到详细信息视图



我是Xcode的新手,现在我正在研究可扩展/可折叠的UITableView。基本思想是在单击第一个主单元格时创建新的子下拉单元格,并且子单元格可以转到详细信息视图。但我希望从这些新的子单元格中获得详细视图。我发现有两种选择:

  • 创建 segue
  • 创建推送视图控制器。

我拖动了一个新的视图控制器并定义了它的情节提要 ID,并尝试以编程方式创建一个 segue 来推送新的细节视图。 但它不起作用。谁能帮我?谢谢。

/

/didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//to change the button image when selecting a row.
UITableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
UIButton *arrowBtn = (UIButton*)[cell viewWithTag:10];
NSDictionary *dic=[self.itemsinTable objectAtIndex:indexPath.row];
if([dic valueForKey:@"list"])
{
    NSArray *listarray=[dic valueForKey:@"list"];
    BOOL isTableExpanded=NO;
    for(NSDictionary *subitems in listarray )
    {
        NSInteger index=[self.itemsinTable indexOfObjectIdenticalTo:subitems];
        isTableExpanded=(index>0 && index!=NSIntegerMax);
        if(isTableExpanded) break;
    }
    if(isTableExpanded)
    {
        [self CollapseRows:listarray];
        [arrowBtn setImage:[UIImage imageNamed:@"down_arrow.png"] forState:UIControlStateNormal];

    }
    else
    {
        NSUInteger count=indexPath.row+1;
        NSMutableArray *arrCells=[NSMutableArray array];
        for(NSDictionary *dInner in listarray )
        {
            [arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
            [self.itemsinTable insertObject:dInner atIndex:count++];
        }
        [self.expandingtableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationTop];
       [arrowBtn setImage:[UIImage imageNamed:@"up_arrow.png"] forState:UIControlStateNormal];
        //This segue is not working...
        UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"detailvcontroller"];
        [self.navigationController pushViewController:controller animated:YES];}}}
detailvcontroller *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"detailvcontroller"];
[self.navigationController pushViewController:controller animated:YES];

您需要在此处检查几件事:

  1. DetailViewController的情节提要标识符设置正确。
  2. self.storyboard已正确初始化。
  3. 根视图具有导航控制器。

我刚刚在下面添加了故事板初始化代码。请确保您的self.storyboard是这样设置的:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
DetailViewController *viewController = (DetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"detailvcontroller"];
[self.navigationController pushViewController:controller animated:YES];

最新更新