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?
-
What issue are you having? Do you know how to load a table view with one set of data? Do you know how to handle a button press? Do you know how to get both sets of data?rmaddy– rmaddy2015-12-06 23:32:19 +00:00Commented Dec 6, 2015 at 23:32
-
I know how to load a table view with one set of data, I know how to handle a button press, I do not know how to get both sets of data.midgyb– midgyb2015-12-06 23:35:07 +00:00Commented Dec 6, 2015 at 23:35
-
Then you need to focus on that first. Get to the point where you can load one set of data or the other. Then you can work on get one or the other based on the selected button.rmaddy– rmaddy2015-12-06 23:36:38 +00:00Commented Dec 6, 2015 at 23:36
-
are you trying to achieve something like a segmentedControl?adolfosrs– adolfosrs2015-12-06 23:58:31 +00:00Commented Dec 6, 2015 at 23:58
-
I think a segmented Contro is the best aproach in this case :)adolfosrs– adolfosrs2015-12-07 00:05:11 +00:00Commented Dec 7, 2015 at 0:05
2 Answers
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:
1 Comment
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.
