My goal is to display data in a tree.

Now, when clicking on a cell, we go deeper in this tree and display it on the next screen (the entire list of subordinates of this cell) I create the same view controller to display these tables and switch between them, depending on the depth, but this is correct, because we cannot know in advance the depth of the tree -> how many such screens will be needed, can we create them programmatically with a specific interface? Or can it all be possible to implement in one table view with such switchings, with the possibility of going back? enter image description here

  • The question is not ash, it is necessary when you click on a cell to move to a new twist with the data of this cell ?? - Victor Mishustin
  • @ ViktorMishustin yes - StriBog
  • In a situation where you do not know exactly how the navigation will occur, you just need to indicate in the storyboard what you want to have in the view and do all the transitions between the screens programmatically. The second option in the answer @ Viktor Mishustin illustrates this. - Max Mikheyenko

2 answers 2

Yes, you can easily create the same viewController (from the code, xib, storyboard) and pass in them arrays for display.

If you do not want to do a lot of controllers, add one section / cell to the first controller (Back or ".."). By clicking on it you go to a higher level. By clicking on the cell with the data you go to the level below.

    There may be several options for a solution.

    1. Use the Singlton programming Singlton . Its purpose is to make a class object a single instance on the system.

    Create a class with the properties you need for an example.

     SingleTone.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface SingleTone : NSObject @property (strong, nonatomic) NSString * titleString; + (id)sharedManager; @end SingleTone.m #import "SingleTone.h" @implementation SingleTone @synthesize titleString; #pragma mark Singleton Methods + (id)sharedManager{ static SingleTone *sharedMyManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedMyManager = [[self alloc] init]; }); return sharedMyManager; } @end 

    Thus, you create a class SingleTone After that, you set the parameters for the property you have registered in the right place.

    In the case of a delegate, in the first controller method of the delegate we prescribe the setter stringArray is an array to fill the headers in the cells.

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[SingleTone sharedManager] setTitleString:[stringArray objectAtIndex:indexPath.row]]; } } 

    Each time you click on a new cell, the data will be overwritten.

    To get a string in the new controller, you need to use the following method.

     NSString * newString = [[SingleTone sharedManager] titleString]; 

    And this line is already inserted where necessary;

    1. In advance to write in property controller to which you pass, data.

    In the new controller in the header you create the property

     @property (strong, nonatomic) NSString * titleString 

    When moving from the first controller, before you push, transfer data.

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ViewControllerSecondTable * detail = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerSecondTable"]; detail.titleString = [stringArray objectAtIndex:indexPath.row]; [self.navigationController pushViewController:detail animated:YES]; } } 

    It will remain only in the new controller to bring this property where you need.

     NSString * newString = self.titleString 

    In general, the transfer options are many, for each case fits your own.

    • the question is not in data transfer, but in how to avoid creating manually a dozen identical screens to switch between them - StriBog
    • 'Singleton' is one word, no need to capitalize on T (and no 'e' at the end) - Max Mikheyenko
    • Thank you, corrected - Victor Mishustin