0

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?

7
  • 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? Commented 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. Commented 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. Commented Dec 6, 2015 at 23:36
  • are you trying to achieve something like a segmentedControl? Commented Dec 6, 2015 at 23:58
  • I think a segmented Contro is the best aproach in this case :) Commented Dec 7, 2015 at 0:05

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Very Helpful!
0

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.

5 Comments

So then I would use an if statement to handle which data is loaded?
Yes, that's one way of solving it.
Or use one variable to store the current datasource for your table. You then update this variable to point to different datasources when the buttons are tapped and then you reload the table. This works best if the datasources all have the same structure/properties, of course.
Ok so would that work better than my proposed if statement? They would have the exact same structure
Yes, in that case you only need one if statement - when deciding what the datasource variable should point to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.