user1555112
user1555112

Reputation: 1977

ios swift: What is the apporach way to reload a tableView?

I am using a tableView with static cells for a kind of quiz game.

Therefore I have all my questions in an array and would like to loop through.

When the user has answered one question, I would like to show him the next question in the same tableView. Therefore I need to pass data from the tableView to "itself" to update the questions counter and some other stuff etc.

The user has a button "next" to go to the next question.

What do you guys suggest me to do?

A self.tableView.reloadData() can't be the solution, as I have to transfer data? It has to be a segue, or?

I tried to have the button "next" as IBAction to perform a segue to itself, but then the segue happens without the doing the code I have in my IBAction.

Thanks for any ideas!!

EDIT:

These vars I need to update:

var totalCardsInt: Int = 0
var currentCardInt: Int = 0
var currentDisplayedCardInt: Int = 1

If I am using self.tableView.reloadData() at which point I have to update these vars in order to have them updated after reload?

Sorry... I am just a little lost right know...

Upvotes: 0

Views: 1302

Answers (3)

user1555112
user1555112

Reputation: 1977

I have to answer my own question - as I was totally wrong in my thoughts... Sometimes it makes sense to sleep over it...

I can use the tableView. All I have to do is to get my "timeline" in the right order. There is no need to reload the tableView I just needed to clear my static cells and fill in new data.

I think it's a little too much to paste the code here but in short words this is my timeline:

  1. Load all questions (and theirs answers) into an array

  2. Fill in data into view eg. displaying the question and possible answers

  3. Add IBAction, proof answer and increase current array index +1. Check if current == total count then go to final view to say "finished"

  4. Re-call Nr.2 to show next question

Upvotes: 0

Rory McKinnel
Rory McKinnel

Reputation: 8014

I do not see why you can not use self.tableView.reloadData()? The cells should be reflecting the contents of your controllers data model. So if you want to show a new set of question data set the data for the cells to be the new set and then reload.

If you want to have a history of questions us an array of questions and use an index into this array for the current question. Make the cells use the question index to populate the cells views. You can then move forward and back by changing the index for the question reloading each time.

Upvotes: 0

Nikos M.
Nikos M.

Reputation: 13783

You should reload your table with self.tableView.reloadData() but before that you should modify the datasource (the array or dictionary which is used to populate the cells with data), so the table loads new data when it reloads.

Upvotes: 3

Related Questions