Kevin
Kevin

Reputation: 39

Load different arrays in the same table view controller based on a button press

I have a view controller and a table view contoller. In my view controller, I have 6 different buttons which when pressed should each load different arrays table view.

I made this diagram:

How should I proceed?

Upvotes: 1

Views: 512

Answers (1)

Alain T.
Alain T.

Reputation: 42139

The solutions to this can range from very simple to more involved depending on how different the contents and behaviours are between your 6 arrays.

Simplest: All six arrays have the same structure and behave exactly the same in the table view.

--> Define a "current" array variable for your table view data source and copy the content of the selected array to it when a button is pressed. Then call reloadData.

A little more complex: All six arrays have the same structure but must show different content in the table view.

--> Use "Current" array variable as above but define multiple prototype cells in the table view. Maintain an internal variable in your view controller to determine which button is currently selected. In the getCellForRowAtIndexPath method, use your internal variable in a switch statement to perform the appropriate dequeuing and mapping of cell contents.

Most complex: Arrays have different structures and must show different content/behaviour.

---> Define a class for each array and implement the datasource and delegate methods specific to the structure and cell content/behaviour that you need. In your buttons, switch the table view's delegate to the corresponding class. (this will help keep the specifics of each array type cleanly separated)

Upvotes: 2

Related Questions