Tales123 Tales123 - 6 months ago 17
iOS Question

How to use SWRevealVC with didSelectRowAtIndexPath and prepareForSegue?

I have a problem with using SWRevealViewController. How to call prepare for segue in didSelectRowAtIndexPath? It is necessary because in tableView I have name of categories and some of this categories have its own subcategories. For example my list:

- category1
- category2
+ category3
- category4


So, when I click on + category3 I should see

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


But I could not to do this because of prepareForSegue
Here is my code:

- (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;
}
}
}


Also, I could not to pass data throw VCs, because prepareForSegue call firstly. Please help me. Thanks!

Answer
//in prepareForSegue method

NSIndexPath *indexPath = [tableView indexPathForSelectedRow];

and you can get selected row value by using indexPath.row