【问题标题】:How to use SWRevealVC with didSelectRowAtIndexPath and prepareForSegue?如何将 SWRevealVC 与 didSelectRowAtIndexPath 和 prepareForSegue 一起使用?
【发布时间】:2015-05-10 07:21:46
【问题描述】:

我在使用 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;
        }
    }
}

另外,我不能传递数据 throw VC,因为 prepareForSegue 首先调用。请帮我。谢谢!

【问题讨论】:

    标签: ios uitableview uistoryboardsegue didselectrowatindexpath swrevealviewcontroller


    【解决方案1】:
    //in prepareForSegue method
    
    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    

    您可以使用 indexPath.row 获取选定的行值

    【讨论】:

    • 好的,谢谢你的回答,但是你能详细描述一下你的意思吗?
    • 我认为你需要在选择 tableviewrow 时将值从某个数组传递到 categoryVC .. 对吗?
    • 好的..你是如何将segue设置为categoryVC的?是来自视图控制器的 tableviewcell 吗?
    • 从 tableviewcell 我将 SWRevealViewControllerSeguePushController 设置为 Navigation 控制器到 categoryVC
    • ok.. 移除那个 segue.. 将 segue 从你的 viewcontroller 添加到 navigationcontroller
    【解决方案2】:

    要更改当前视图,您不应调用 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 可能会发生视图确实发生了应有的更改,但 RevealMenu 仍然可见。对于该问题,您可以致电:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      相关资源
      最近更新 更多