根据我的示例,我必须在 didSelectRowAtIndexPath 方法中为 navigate/push 和新的 ViewController 编写什么代码?
What code I have to write in didSelectRowAtIndexPath method for navigate/push to new ViewController, according to my example?
我指的是 GitHub 的项目。
我正在尝试根据我的要求修改它,但我无法理解我必须在 didSelectRowAtIndexPath
方法中编写什么代码。
我想要的是点击tableView行后它可以是0级或1级(here level 0 means - row has no sub child and level 1 means row has subview/child rows )
当我单击 0 级或 1 级的任何 tableView 行时,它必须导航到下一个 viewController。
在此示例中,行号 2 和 3 是可扩展的,即它包含级别 1 行,
第 0、1、4 和 5 行是它们不会扩展的 1 级行。
Here is project link (updated project link)
***** 添加代码 ****
我在AppDelegate中写的代码class,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
userDefaults=[NSUserDefaults standardUserDefaults];
mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"loginSuccess"]) {
NSLog(@"Login Done!!!");
HomeViewController *homeVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"homeId"];
SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:homeVC];
//SWRevealViewController * vc= [[SWRevealViewController alloc]init];
//ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"sideMenu"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
}else
{
LoginViewController *loginVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginId"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = loginVC;
[self.window makeKeyAndVisible];
}
return YES;
}
而在LoginViewController中class,我写了代码
- (IBAction)loginButtonMethod:(id)sender {
[self->userdefaults setBool:YES forKey:@"loginSuccess"];
InboxView *inboxVC=[self.storyboard instantiateViewControllerWithIdentifier:@"id1"];
[self.navigationController pushViewController:inboxVC animated:YES];
[[self navigationController] setNavigationBarHidden:NO];
}
试试这个。 ProductVC 是您要推送的 class
NSString * storyboardName = @"Main";
NSString * viewControllerID = @"id3";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
ViewController3 * controller = (ViewController3 *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller];
[navController setViewControllers: @[controller] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
因此,您可以像这样移动到任何 class,在 didselectrowatindexpath 中只需验证您想要推送哪个 class 的行。
请在以下方法中编写您的父级导航代码。对于子视图
- (void)ftFoldingTableView:(FTFoldingTableView *)ftTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"indexPath--->%ld",(long)indexPath.row);
//Write your push next view controller code
[ftTableView deselectRowAtIndexPath:indexPath animated:YES];
}
按照方法编写父视图导航代码
- (void)ftFoldingTableView:(FTFoldingTableView *)ftTableView willChangeToSectionState:(FTFoldingSectionState)sectionState section:(NSInteger)section
{
NSLog(@"section-->%ld",(long)section);
// Write push level 0 or parent view move next view controller code
// NSLog(@"section: %ld is about to %@", section, sectionState == FTFoldingSectionStateFold ? @"close" : @"open");
}
试试这个代码是否正常工作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//check if the row at indexPath is a child of an expanded parent.
//At a time only one parent would be in expanded state
NSInteger indexSelected = indexPath.row;
NSDictionary *dicSelected =[self.itemsInTable objectAtIndex:indexSelected];
/**
Resolved the issue of top line separator dissapearing upon selecting a row (hackishly) by reloading the selected cell
*/
@try {
[self.menuTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@catch (NSException *exception) {
}
[tableView setSectionIndexBackgroundColor:[UIColor blackColor]];
//
//Collapse any other expanded cell by iterating through the table cells.
//Don't collapse if the selected cell is a child of an expanded row.
NSDictionary *dicCellToCollapse =[self.itemsInTable objectAtIndex:g_ExpandedCellIndex];
NSInteger numRowsCollapsed = 0;
NSInteger level = [[dicSelected valueForKey:@"level"] integerValue];
if(level==0 && [dicCellToCollapse valueForKey:@"SubItems"] && (g_ExpandedCellIndex != indexPath.row))
{
NSArray *arr=[dicCellToCollapse valueForKey:@"SubItems"];
BOOL isTableExpanded=NO;
for(NSDictionary *subitems in arr )
{
NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
isTableExpanded=(index>0 && index!=NSIntegerMax);
if(isTableExpanded) break;
}
//
//Collapse the parent cell if its expanded
if(isTableExpanded)
{
[self CollapseRows:arr];
numRowsCollapsed = [arr count]-1;
}
}
//
//go about the task of expanding the cell if it has subitems
NSDictionary *dic;
if (g_ExpandedCellIndex < indexPath.row && numRowsCollapsed)
{
dic = [self.itemsInTable objectAtIndex:indexPath.row-numRowsCollapsed-1];
}
else
{
dic = [self.itemsInTable objectAtIndex:indexPath.row];
}
//
//Check if the selected cell has SubItems i.e its a Parent cell
if([dic valueForKey:@"SubItems"])
{
arr=[dic valueForKey:@"SubItems"];
BOOL isTableExpanded=NO;
for(NSDictionary *subitems in arr )
{
NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
isTableExpanded=(index>0 && index!=NSIntegerMax);
if(isTableExpanded) break;
}
//
//Collapse the parent cell if its expanded
if(isTableExpanded)
{
[self CollapseRows:arr];
}
//
//Else expand the cell to show SubItems
else
{
// ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
// [self.navigationController pushViewController:vc animated:YES];
//
//store the location of the cell that is expanded
NSUInteger rowIndex;
if (g_ExpandedCellIndex < indexPath.row && numRowsCollapsed)
{
rowIndex = indexPath.row-numRowsCollapsed;//zero based index
g_ExpandedCellIndex = indexPath.row-numRowsCollapsed-1;//zero based index
}
else
{
rowIndex = indexPath.row+1;
g_ExpandedCellIndex = indexPath.row;
}
//
//Insert the SubItems
NSMutableArray *arrCells=[NSMutableArray array];
for(NSDictionary *dInner in arr )
{
[arrCells addObject:[NSIndexPath indexPathForRow:rowIndex inSection:0]];
[self.itemsInTable insertObject:dInner atIndex:rowIndex++];
ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self.navigationController pushViewController:vc animated:YES];
}
[self.menuTableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationTop];
}
}
//Else a subItem has been clicked. We need to push the relevant view into segue
else
{
NSString* strId =[dic valueForKey:@"id"];
NSDictionary *dict = arr[0];
NSString *id1=[dict objectForKey:@"id"];
NSLog(@"%@",strId);
NSLog(@"%@",id1);
if([id1 isEqualToString:@"sme_marketwatch"])
{
ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self.navigationController pushViewController:vc animated:YES];
}
ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self.navigationController pushViewController:vc animated:YES];
//
// ViewController1 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id1"];
// [self.navigationController pushViewController:vc animated:YES];
// NSString* strId =[dic valueForKey:@"id"];
// NSLog(@"Id is %@",strId);
//
// if([strId isEqualToString:@"getquote"]) //getquote
// {
// ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
// [self.navigationController pushViewController:vc animated:YES];
// }
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController3 *newView = [storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self presentViewController:newView animated:YES completion:nil];
// ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
// [self.navigationController pushViewController:vc animated:YES];
}
我指的是 GitHub 的项目。
我正在尝试根据我的要求修改它,但我无法理解我必须在 didSelectRowAtIndexPath
方法中编写什么代码。
我想要的是点击tableView行后它可以是0级或1级(here level 0 means - row has no sub child and level 1 means row has subview/child rows )
当我单击 0 级或 1 级的任何 tableView 行时,它必须导航到下一个 viewController。
在此示例中,行号 2 和 3 是可扩展的,即它包含级别 1 行, 第 0、1、4 和 5 行是它们不会扩展的 1 级行。
Here is project link (updated project link)
***** 添加代码 ****
我在AppDelegate中写的代码class,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
userDefaults=[NSUserDefaults standardUserDefaults];
mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"loginSuccess"]) {
NSLog(@"Login Done!!!");
HomeViewController *homeVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"homeId"];
SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:homeVC];
//SWRevealViewController * vc= [[SWRevealViewController alloc]init];
//ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"sideMenu"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
}else
{
LoginViewController *loginVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginId"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = loginVC;
[self.window makeKeyAndVisible];
}
return YES;
}
而在LoginViewController中class,我写了代码
- (IBAction)loginButtonMethod:(id)sender {
[self->userdefaults setBool:YES forKey:@"loginSuccess"];
InboxView *inboxVC=[self.storyboard instantiateViewControllerWithIdentifier:@"id1"];
[self.navigationController pushViewController:inboxVC animated:YES];
[[self navigationController] setNavigationBarHidden:NO];
}
试试这个。 ProductVC 是您要推送的 class
NSString * storyboardName = @"Main";
NSString * viewControllerID = @"id3";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
ViewController3 * controller = (ViewController3 *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller];
[navController setViewControllers: @[controller] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
因此,您可以像这样移动到任何 class,在 didselectrowatindexpath 中只需验证您想要推送哪个 class 的行。
请在以下方法中编写您的父级导航代码。对于子视图
- (void)ftFoldingTableView:(FTFoldingTableView *)ftTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"indexPath--->%ld",(long)indexPath.row);
//Write your push next view controller code
[ftTableView deselectRowAtIndexPath:indexPath animated:YES];
}
按照方法编写父视图导航代码
- (void)ftFoldingTableView:(FTFoldingTableView *)ftTableView willChangeToSectionState:(FTFoldingSectionState)sectionState section:(NSInteger)section
{
NSLog(@"section-->%ld",(long)section);
// Write push level 0 or parent view move next view controller code
// NSLog(@"section: %ld is about to %@", section, sectionState == FTFoldingSectionStateFold ? @"close" : @"open");
}
试试这个代码是否正常工作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//check if the row at indexPath is a child of an expanded parent.
//At a time only one parent would be in expanded state
NSInteger indexSelected = indexPath.row;
NSDictionary *dicSelected =[self.itemsInTable objectAtIndex:indexSelected];
/**
Resolved the issue of top line separator dissapearing upon selecting a row (hackishly) by reloading the selected cell
*/
@try {
[self.menuTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@catch (NSException *exception) {
}
[tableView setSectionIndexBackgroundColor:[UIColor blackColor]];
//
//Collapse any other expanded cell by iterating through the table cells.
//Don't collapse if the selected cell is a child of an expanded row.
NSDictionary *dicCellToCollapse =[self.itemsInTable objectAtIndex:g_ExpandedCellIndex];
NSInteger numRowsCollapsed = 0;
NSInteger level = [[dicSelected valueForKey:@"level"] integerValue];
if(level==0 && [dicCellToCollapse valueForKey:@"SubItems"] && (g_ExpandedCellIndex != indexPath.row))
{
NSArray *arr=[dicCellToCollapse valueForKey:@"SubItems"];
BOOL isTableExpanded=NO;
for(NSDictionary *subitems in arr )
{
NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
isTableExpanded=(index>0 && index!=NSIntegerMax);
if(isTableExpanded) break;
}
//
//Collapse the parent cell if its expanded
if(isTableExpanded)
{
[self CollapseRows:arr];
numRowsCollapsed = [arr count]-1;
}
}
//
//go about the task of expanding the cell if it has subitems
NSDictionary *dic;
if (g_ExpandedCellIndex < indexPath.row && numRowsCollapsed)
{
dic = [self.itemsInTable objectAtIndex:indexPath.row-numRowsCollapsed-1];
}
else
{
dic = [self.itemsInTable objectAtIndex:indexPath.row];
}
//
//Check if the selected cell has SubItems i.e its a Parent cell
if([dic valueForKey:@"SubItems"])
{
arr=[dic valueForKey:@"SubItems"];
BOOL isTableExpanded=NO;
for(NSDictionary *subitems in arr )
{
NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
isTableExpanded=(index>0 && index!=NSIntegerMax);
if(isTableExpanded) break;
}
//
//Collapse the parent cell if its expanded
if(isTableExpanded)
{
[self CollapseRows:arr];
}
//
//Else expand the cell to show SubItems
else
{
// ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
// [self.navigationController pushViewController:vc animated:YES];
//
//store the location of the cell that is expanded
NSUInteger rowIndex;
if (g_ExpandedCellIndex < indexPath.row && numRowsCollapsed)
{
rowIndex = indexPath.row-numRowsCollapsed;//zero based index
g_ExpandedCellIndex = indexPath.row-numRowsCollapsed-1;//zero based index
}
else
{
rowIndex = indexPath.row+1;
g_ExpandedCellIndex = indexPath.row;
}
//
//Insert the SubItems
NSMutableArray *arrCells=[NSMutableArray array];
for(NSDictionary *dInner in arr )
{
[arrCells addObject:[NSIndexPath indexPathForRow:rowIndex inSection:0]];
[self.itemsInTable insertObject:dInner atIndex:rowIndex++];
ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self.navigationController pushViewController:vc animated:YES];
}
[self.menuTableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationTop];
}
}
//Else a subItem has been clicked. We need to push the relevant view into segue
else
{
NSString* strId =[dic valueForKey:@"id"];
NSDictionary *dict = arr[0];
NSString *id1=[dict objectForKey:@"id"];
NSLog(@"%@",strId);
NSLog(@"%@",id1);
if([id1 isEqualToString:@"sme_marketwatch"])
{
ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self.navigationController pushViewController:vc animated:YES];
}
ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self.navigationController pushViewController:vc animated:YES];
//
// ViewController1 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id1"];
// [self.navigationController pushViewController:vc animated:YES];
// NSString* strId =[dic valueForKey:@"id"];
// NSLog(@"Id is %@",strId);
//
// if([strId isEqualToString:@"getquote"]) //getquote
// {
// ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
// [self.navigationController pushViewController:vc animated:YES];
// }
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController3 *newView = [storyboard instantiateViewControllerWithIdentifier:@"id3"];
[self presentViewController:newView animated:YES completion:nil];
// ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"id3"];
// [self.navigationController pushViewController:vc animated:YES];
}