Determine content to fill Uitableview based on a Uibutton tag

Asked

Viewed 79 times

1

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

The buttons are as follows::

Button Restaurants -> Will open a UITableView with a different restaurant in each cell. Store button -> Will open a UITableView with a store for each cell. Place button -> Will open a UITableView with different places in each cell. Hotel button -> Will open a UITableView with a particular hotel in each cell.

With this scenario, I would need to create a UIViewController for each screen and its respective UITableView. What ends up "polluting" the code and the Storyboard 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 using the property tag of UIButton to distinguish buttons and press 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 doubt about the issue just leave a comment that I can explain again if necessary.

  • Your views are being made with storyboard or xib?

  • I am using Storyboard.

1 answer

2


Well like you said, you don’t even have to have several controllers for each different content. Let’s assume 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 the segue 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];
        }
    }
}

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

I believe this also answers your another question.

I hope it helped.

  • Thank you @Paulo-Rodrigues!! I’ll test and tell you how it turned out!

  • when you created Enum, you set 0 to T_RESTAURANTES, but the others? Should they follow the ascending order? 1, 2... ?

  • Actually not needed, it was just an example, because defining the first the next elements will be incremented. If the first was 5, the rest would be 6, 7, 8 etc.

  • Paulo vc uses skype? me add la if you can speak: [email protected]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.