Setting the name of a dynamic view from a table view

I use a combination of SWRevealViewController, ContentViewController and NavigationViewController (Navigation).

The storyboard is as follows:

current status of the storyboard

In a TableViewCell application I can get the navigation text. What I want to do is return this information to myself. Set the ContentViewController header to the selected cell and load other desired data.

How can I perform this function?

Here is my last code in the NavigationViewController.m file:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];


    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (isChild) {
        NSString *cellText = selectedCell.detailTextLabel.text;
        NSLog(@"%@ tapped",cellText);
        return;
    }
    else{
        NSString *cellText = selectedCell.textLabel.text;
        NSLog(@"%@ tapped",cellText);

        if([cellText isEqualToString:@"Sign Out"]){
            [self performSegueWithIdentifier:@"goToHome" sender:self];
        }
        else if ([cellText isEqualToString:@"Sign In"]){
            [self performSegueWithIdentifier:@"goToHome" sender:self];
        }
        else if ([cellText isEqualToString:@"Festival Map"]){
            [self performSegueWithIdentifier:@"festivalMap" sender:self];
        }
    }

    [self.tableView beginUpdates];

    if (currentExpandedIndex == indexPath.row) {
        [self collapseSubItemsAtIndex:currentExpandedIndex];
        currentExpandedIndex = -1;
    }
    else {

        BOOL shouldCollapse = currentExpandedIndex > -1;

        if (shouldCollapse) {
            [self collapseSubItemsAtIndex:currentExpandedIndex];
        }

        currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

        [self expandItemAtIndex:currentExpandedIndex];
    }

    [self.tableView endUpdates];

}



- (void)expandItemAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    NSArray *currentSubItems = [subItems objectAtIndex:index];
    int insertPos = index + 1;
    for (int i = 0; i < [currentSubItems count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
    }
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

- (void)collapseSubItemsAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"festivalMap"]){
        NSLog(@"TEST");

        NSString * title = @"CHANGE";

        ViewController * view = [segue destinationViewController];
        view.navigationItem.title = title;
    }
}

And here is the code for my ContentViewController.m file:

#import "ContentViewController.h"
#import "SWRevealViewController.h"
#import "Function.h"

@interface ContentViewController ()

@end

@implementation ContentViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _barButton.target = self.revealViewController;
    _barButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

@end

thank

+4
source share
1 answer

:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if([segue.identifier isEqualToString:@"yoursegue"]){  
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
    ContentViewController*contentView = (ContentViewController*)segue.destinationViewController;
    contentView.cellText = selectedCell.textLabel.text; 
    NSLog(@"cellText  = %@",selectedCell.textLabel.text;);
  }
}

, - , . MVC.

0

Source: https://habr.com/ru/post/1584504/


All Articles