如何在didSelectRowAtIndexPath和prepareForSegue中使用SWRevealVC?

时间:2015-03-09 07:18:58

标签: ios uitableview uistoryboardsegue didselectrowatindexpath swrevealviewcontroller

使用SWRevealViewController时遇到问题。如何在didSelectRowAtIndexPath中调用segue的准备?这是必要的,因为在tableView中我有类别的名称,其中一些类别有自己的子类别。例如我的清单:

- category1
- category2
+ category3
- category4

所以,当我点击+ category3时,我应该看到

- category1
- category2
+ category3
  - subcategory1
  - subcategory2
- category4

但由于prepareForSegue,我不能这样做 这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section <= _categories.count) {
        GDCategory *category = [[_subCategories objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        _gdCategory = category;
        if (category.subcategories.count > 0) {
            NSNumber *categoryId = @(category.dbId);
            if ([_selectedSubCategories containsObject:categoryId]) {
                [_selectedSubCategories removeObject:categoryId];
            } else {
                [_selectedSubCategories addObject:categoryId];
            }
            NSLog(@"There are %lu subcategories in %@", (unsigned long)category.subcategories.count, category.name);
            [self recalcSubcategories];
            [tableView reloadData];
        } else {
            //Here I should call prepareForSegue
        }
        NSLog(@"selected category is %@", category.name);
    }
}

- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender {
    // configure the destination view controller:
    if ( [sender isKindOfClass:[UITableViewCell class]] ) {
        UINavigationController *navController = segue.destinationViewController;
        GDCategoryVC *categoryVC = [navController childViewControllers].firstObject;
        if ( [categoryVC isKindOfClass:[GDCategoryVC class]] ) {
            categoryVC.selectedCategory = _gdCategory;
        }
    }
}

另外,我无法传递数据抛出VC,因为prepareForSegue首先调用。请帮我。谢谢!

2 个答案:

答案 0 :(得分:0)

//in prepareForSegue method

NSIndexPath *indexPath = [tableView indexPathForSelectedRow];

您可以使用indexPath.row

获取选定的行值

答案 1 :(得分:0)

要更改当前视图,您不应该调用prepareForSegue。请改用performSegueWithIdentifier。 PrepareForSegue将自动调用。 如果要将UITableViewCell作为发送方传递,则需要从TableViewDataSource调用cellForRowAtIndexPath

else {
            //Here I should call prepareForSegue
            UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath] //call self if dataSource is implement in your class
           [self performSegueWithIdentifier:segueIdentifier sender:cell] // Insert your identifier for the wished Segue
        }

使用SWRevealController可能会发生View确实发生的变化,但RevealMenu仍然可见。对于该问题,您可以致电:

[self.revealViewController setFrontViewPosition:FrontViewPositionLeft animated:YES];