midgyb
midgyb

Reputation: 25

Loading Data in sameTableViewController based on button press Swift

I'm working on a sample iOS app and want to load (in a tableViewController) certain data based on the button the user presses at the top of the view. For example, when the user loads the page it will by default load data from "myPosts" but on the top of the view there will be two buttons "My Posts", "Not My Posts". If they then select the "Not My Posts" button then the tableview cells will be reloaded with the data from "notMyPosts". Anybody know how to do this?

Upvotes: 0

Views: 288

Answers (2)

adolfosrs
adolfosrs

Reputation: 9389

So, the best solution here is to use a Segmented Control. Basically, you will have a controller for each TableView and a controller for the SegmentedControl.

This tutorial and this question gives a basic idea on the segmentedControl and how to set the basic controller for it. You will basically have a Container View with a view for each tableView you want to display and show/hide it based on the selected segment on the SegmentedControl.

This is how your storyboard will look like:

enter image description here

Upvotes: 1

rodskagg
rodskagg

Reputation: 3947

Just use two different datasources for your table, depending on which button the user selects.

So, when the user taps on a button set a variable to keep track of which button they tapped, and then reload your tableview.

Upvotes: 0

Related Questions