如何将 SWRevealVC 与 didSelectRowAtIndexPath 和 prepareForSegue 一起使用?
How to use SWRevealVC with didSelectRowAtIndexPath and prepareForSegue?
我在使用 SWRevealViewController 时遇到问题。如何在 didSelectRowAtIndexPath 中调用 prepare for 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 首先调用。请帮我。谢谢!
//in prepareForSegue method
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
并且您可以使用 indexPath.row
获取选定的行值
要更改当前视图,您不应调用 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];
我在使用 SWRevealViewController 时遇到问题。如何在 didSelectRowAtIndexPath 中调用 prepare for 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 首先调用。请帮我。谢谢!
//in prepareForSegue method
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
并且您可以使用 indexPath.row
获取选定的行值要更改当前视图,您不应调用 prepareForSegue。请改用 performSegueWithIdentifier。 PrepareForSegue 将被自动调用。 如果您想将 UITableViewCell 作为发件人传递,您需要从 TableViewDataSource
调用 cellForRowAtIndexPathelse {
//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];