Determining content to fill UITableView based on a UIButton tag

1

I will explain the scenario to be as clear as possible to understand the issue:  In the main screen of my app I have 4 UIButton and each one of them will open a different screen containing each one its own UITableVIew and its contents.

The buttons are as follows:

Restaurants button - > It will open UITableView with a different restaurant in each cell. Stores Button - > It will open a UITableView with one store for each cell. Places Button - > It will open UITableView with distinct places in each cell. Hotel Button - > It will open UITableView with a particular hotel in each cell.

With this scenario, I would need to create a UIViewController for each screen and its UITableView . What ends up "polluting" the Storyboard code and screen.

I would like to know a way to use only a single screen to display (with UITableView implemented) to display these contents separately, according to the button chosen, since the display layout will follow the same pattern. The solution I thought would be to use the tag property of UIButton to distinguish the buttons and load the appropriate content.

In case what I want to know is: How to show different content using the same UITableView , distinguishing the content to be shown through the selected button?

Any questions about the issue just leave a comment that I can explain again if necessary.

    
asked by anonymous 23.09.2014 / 04:59

1 answer

2

Just like you said, you do not even have to have multiple controllers for each different content. Let's say you have these buttons:

@property (nonatomic, weak) IBOutlet UIButton *btnRestaurantes;
@property (nonatomic, weak) IBOutlet UIButton *btnLojas;
@property (nonatomic, weak) IBOutlet UIButton *btnLugares;

These same buttons respond to a single action:

- (IBAction)abrirConteudo:(UIButton *)sender {
    [self performSegueWithIdentifier:@"abrirConteudo" sender:sender];
}

I also created a enum for each type:

enum tipos {
    T_RESTAURANTES = 0,
    T_LOJAS,
    T_LUGARES
};

And while preparing% of your storyboard :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"abrirConteudo"]) {
        ViewConteudoController *viewConteudo = [segue destinationViewController];

        UIButton *button = (UIButton *)sender;

        if ([button isEqual:[self btnRestaurantes]]) {
            [viewConteudo setTipoConteudo:T_RESTAURANTES];
        } else if ([button isEqual:[self btnLojas]]) {
            [viewConteudo setTipoConteudo:T_LOJAS];
        } else if ([button isEqual:[self btnLugares]]) {
            [viewConteudo setTipoConteudo:T_LUGARES];
        }
    }
}

Now, at your destination, which I have here called segue , the variable ViewConteudoController receives one of these values and from it you decide what to display in the contents of your table.

I think this also answers your other question .

I hope you have helped.

    
23.09.2014 / 14:38